I have an android app which is a simple home screen. What I want is to create an option where users can select background by themselves. Currently I have made the option but it does not hold the background after I restart or press back button. It goes back to the background what I set in the xml file as android:background. Is there any way out? I heard by using shared preference it's possible. but don't know how to do it. Can any one help on this?
Assuming you have several backgrounds available for them to choose and they select one and you want to simply store the id of the background they have chosen you would do this:
SharedPreference sp = getSharedPreferences("uniqueString", 0);
Editor editor = sp.edit();
editor.putInt("userBG", 5);// this is assuming the user picked your 5th bg
editor.commit();
Then onCreate in your activity you can load the SharedPreference again and...
int userPic = sp.getInt("userBG", 0); //this will either pull a previously saved number or return 0 (or whatever number you choose to provide as the default there.
That's it.
Here's the example from google, in case my snippet isn't sufficient...
http://developer.android.com/guide/topics/data/data-storage.html
Note that if you're storing an int, you can store the drawable id of the background itself, which saves you one step of translation. You can then just pull the resource directly into the background of your parent layout.
Related
So at the moment the user will select a colour, shape and time. When the app first starts up there is a default for each. What I want to happen is to keep the users selection for all 3 of these even after they close the app while it runs in the background.
If you need me to post anything to help ask away. Thanks.
After writing out what the problem was, it's not that they're not being saved it's the fact that the selections ARE being saved but not being displayed properly. I.E by a border or the dropdown menu results back to it's default value instead of the users selection.
So the values are being saved(my bad) but the app overwrites these saves and doesn't use the border or beings the dropdown menu back to it's default value.
circularImageView = (CircularImageView)findViewById(R.id.activity_main_silver_color_button);
circularImageView.setBorderColor(getResources().getColor(R.color.unselected_border));
circularImageView = (CircularImageView)findViewById(v.getId());
circularImageView.setBorderColor(getResources().getColor(R.color.selected_border));
These work independent of each other but when the border is selected, if a another color was selected then it becomes an unselected border. But what happens if "selected_border" state is never saved.
public static void saveLockScreenDuration(Context context, int duration){
getStoredPreferences(context).edit().putInt(SELECTED_DURATION, duration).apply();
public static int getLockScreenDuration(Context context){
return getStoredPreferences(context)
.getInt(SELECTED_DURATION, DEFAULT_DURATION);
}
This is where all the prefereces are stored. DEFAULT_DURATION and SELECTED_DURATION are in a class called constants.
Upon selection you can store selected values in Shared Preferences. And upon launching it again you can check if those values are null or not. And display accordingly. Read how to use Shared Preferences here.
if you talking about for activity restore data.
then see here.
https://developer.android.com/guide/components/activities/activity-lifecycle.html#saras
Correct me if i am wrong. I think you can use PreferenceManager.getDefaultSharedPreferences(Context context)
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
which is shared across all your Activity and Service classes. If you set an accessibility service to get the event when app is in background and used defaultsharedprefrences to store i think it will work for your case.
So i'm planning to develop an android application. In one activity, if i were to click a button of add, then it will prompt a user input and store the data. After that, thare would be another "list of item" button, where users can see which item they added and the information they input earlier.
I was thinking of using if.. else statement to check whether the button is click by setting a variable of default status=0 and change to 1 when clicked.
But how am i going to make sure that the functions won get messed up? is there any better suggestion for this? Im a beginner who just started to explore android studio.
Okay, so it is very simple and logical, and here is my best explanation. It's a rather long explanation, but I made it as thorough as possible...If you need any more help, just let me know:
You don't need to do any of that status=0 stuff. Just have an onclick listener:
(androidfromhome.com)
And in the listener, you want to go to another screen with a text input. So, just have an intent in the onClick which takes user to another screen:
(www.devcfgc.com)
Okay, so now you got your second screen right? You're going to want a textview (Change this in the xml) in the middle of the screen, where the user can enter data:
String stuff = TextView.getText();
Note, you will need to initialize the textview:
(androidfromhome.com)
So, now you got the text that the user entered. Just save it in SharedPreferences, and that's it, you have it saved!
http://developer.android.com/reference/android/content/SharedPreferences.html
Read about SharedPreferences in the link above.
So now, you have it saved. If you are going to get a lot of data, you can also save it in a database, which is more advanced stuff, that I don't know how to do. Now, display the saved data on the screen (A new screen, again, using an intent) using the method:
setText();
on your textView object for the next activity. That's it!
i am developing a android app which basically has 3 type of users client, driver, admin so when user install app it will launch a activity for the selection of user type means 3 buttons on it client, driver or admin. when user select a type he/she will be pushed to that specific activity for example if he selects client then he will be pushed to ClientActivity. and when ever he launches the app after first time he will automatically pushed to ClientActivity. first activity that gives option for user selection will online display first time when app install. i already did this in my ios app with use of userdefaults to save a variable and retrieving it from phone memory. so i need suggestions how can i achieve this maybe a stupid question but i am new with android development. so any kind of help will be appreciated :)
Thanks.
You can use Shared preferences,
When the activity creates check a boolean value that saved in shared preference and based on the value u can use.
For declaration:
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Editor editor = pref.edit();
to save value
editor.putInt("key_name", 0);
editor.commit();
to view the data
int value=pref.getInt("key_name", 0);
based on the value u can pass the specific intent.
android has sharedprefereces concept same as userdefaults in ios. You can use in application specific context or component level context. I suggest better application level context.This shareprefereces values can primitives, objects wont support. And these values will be available for you untill application is unistalled.
I'd like to know how I can get the uri of the current background image of a blackberry.
I need this because I want to edit this picture and then set it as the home screens background again. The edit I want to make is to put text on the image.
So basically I want to perform these steps:
Get the uri of the current
background of the main screen.
Create a new bitmap based on the
background.
Add text to the bitmap
Save the new bitmap and set it as the home background
So, does anyone knows how to get the background uri of the home screen? (not the application background).
You can't get background image. HomeScreen class in the API lets you only to set the background image. To give you an idea, several applications that adds text to background was pushing users to select the background first. I recommend you to do so and after first run of your application, you can save the path.
While searching this question I have seen this. One time Android setup screen?
It asks my question, but I can never get it to work. What I want to happen is when I first startup the app, it gives me the setup screen. Then when I press save, I want it to quit the app. When I click the app again, I want it to preform a task, rather than show up with a screen. So thats really 2 questions, how to make it show a setup screen one time, and then how to make it do an action(by clicking on the app) without a screen showing up at all.
Right now, I use SharedPreferences editor to input my settings.
Pretty simple, in the onCreate of your activity:
savedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
if(savedPreferences.getBoolean(PREF_SHOW_ABOUT_ON_APP_START, true)){
Intent intent = new Intent(this, SetupActivity.class);
startActivity(intent);
savedPreferences.edit().putBoolean(PREF_SHOW_ABOUT_ON_APP_START, false).commit(); // YOu could do this line within the SetupActivity to ensure they have actually done what you wanted
finish();
} else {
// Go somewere else
}
You don't have to activity switch, but you get the picture
For the first time only set up screen, you simply have to use SharedPreferences. Check if a value, alreadyLaunched exists in the shared preferences. If not, show the set up screen. If yes show your app.
For the second part of your question, it would be considered very bad practice to have an icon app that does nothing at all when you click it. How would the user know that it worked or did what it was supposed to do ?
For the "I want a task to execute but I don't want the user to see it happening", I believe you should look at services. When your user closes the app clicking the save button on the set up screen, start a service ( http://developer.android.com/reference/android/app/Service.html )
When the user clicks again on your app, simply show a message and make the app call the service via an intent.
Probably you are forgetting to call
myEditor.commit()
after you are done with setting your preferences.