I have a settings.xml file that contains Preferences for my app. All the values by default are set to "true" and its used by PreferenceActivity in my app.
In my main activity I read the values through
SharedPreferences sp=PreferenceManager.getDefaultSharedPreferences(appContext);
Boolean key = sp.getBoolean("M", false);
it gets me whatever I need at any run except the first time. Only when I open my app for the first time and doesn't open my settings menu I get "false". I mean I have to open menu and only after that the app run correctly.
Any suggestions?
Because yor are getting the default value for first time.
that is false -->
Boolean key = sp.getBoolean("M", false);
use this
Boolean key = sp.getBoolean("M", true);
Use sp.getBoolean("M", true); instead. Default values in preferences file is what it will be when preferences activity is first started. SharedPreferences know nothing about your settings.xml.
Alternatively you can have a separate defaults.xml file (or any other name) containing all the default values for all preferences. Then you use these values in both settings.xml (#bool/default_M_value) and your application (sp.getBoolean("M", getResources().getBoolean(R.bool.default_M_value))).
I personally would stick to first approach though.
Related
I write an application, I want to get a phone number, but getline1number() not working on any devices.
So, I want to create a pop-up to enter a phone number and submit to save and don't show in next time open app.
Like this:
You can always use SharedPreferences to do such things:
SharedPreferences sp = getSharedPreferences("FirstTimeFile", Context.MODE_PRIVATE);
/**
* when the app is opened for the first time, no such variable
* (appIsOpenedForTheFirstTime) exists. So, it becomes true.
*/
boolean appIsOpenedForTheFirstTime = sp.getBoolean("IsAppOpenedForFirstTime",true);
//since it is true, it will be set to false after the execution of following block:
if(appIsOpenedForTheFirstTime) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("IsAppOpenedForFirstTime", false);
editor.commit();
//PUT THE CODE FOR YOUR POPUP HERE
}
As the SharedPreferences values remain in the application data even after you close the app, so the next time you open the app, the value of appIsOpenedForTheFirstTime will be false and hence your pop-up code won't be executed.
Ah, as a side-note, if you clear the app data, everything gets cleared - including the SharedPreferences. Read this official article for in-depth understanding.
I am trying to develop an app that requires certain values to be set by the users at the app's first startup only, because i don't wanna bother them frequently inputting the values everytime they launch the app. My app has a single activity main and uses certain values that are inputted by the users at first startup. How can I make this possible.
Please explain me elaborately . :-)
You should use SharedPreferences to keep a track of the first use.
In the onCreate Method of your Activity (Startup activity), you could do something like this,
SharedPreferences userPrefs = getSharedPreferences("UserPrefs", 0);
Boolean firstUse = userPrefs.getBoolean("firstUse", true);
if(firstUse){
//this implies it is the first use of the app
//also once you are done implementing the logic for first use you need to put firstUse as true
SharedPreferences.Editor editor = userPrefs.edit();
editor.putBoolean("firstUse", false);
editor.commit();
}
else{
//take the user directly inside the app
}
Also, if you plan to save user information in the first use, look at different ways of storing data here.
show the alert initially and after getting the input values keep it in preference and next time check whether the required values existing or not. If it is already there avoid popup
For getting more information about shared preference check this link http://www.vogella.com/tutorials/AndroidFileBasedPersistence/article.html
preferences_statusFirst.getString("boot", "");
if (status.length() <= 0)
{
showDialog(DIALOG_birth);
editor_boot.putString("boot", "1");
editor_boot.commit();
}
else
{
}
}
I have a problem in the LiveWallpaper app (my first) I am developing.
Consider 2 classes: LiveWallpaperService and LiveWallpaperSettings.
LiveWallpaperSettings extends PreferencyActivity. Example data representing
the preferences selected by the user, for example a boolean displaySprite (true=> display the sprite on the screen, false do not display) are saved/persisted via SharedPreferences in LiveWallpaperSettings.
Upon starting the application (Settings -> Display -> LiveWallpaper -> MyLiveWallpaper), the saved preferences need to be known so that the sprite can be displayed or not.
However, LiveWallpaperSettings is not instantiated until the Settings button is clicked, so SharedPreferences is not available, and thus saved settings are unavailable until then.
I tried this in LiveWallpaperService.onCreateScene(), but it has no data in it:
SharedPreferences startupPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
What can I do?
There is a default value if the entry/sharedpref file does not exist:
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
boolean display = settings.getBoolean("display", true);
"public abstract boolean getBoolean (String key, boolean defValue)"
Added in API level 1
Retrieve a boolean value from the preferences.
Parameters
key The name of the preference to retrieve.
defValue Value to return if this preference does not exist.
Hope I didnt misunderstood your question :)
In your preferences xml set the default value and in your MainActivity onCreate() add the following code setDefaultValues(this, R.xml.yourxmlname, false);
I have an application which has a Prefernces Class and I want to know how could i make so that when the application is started the settings to be applied even before entering the preferences ( settings ) class. I have a getPrefs() void method which is called when i press "Save" Button in preference activity.
So, could you help me and tell what should I do the "default" preferences to be applied when entering the application ? (I need getprefs method from another class )
I would be grate if you could give me some advices or tips.Thank you !
To get an instance of the SharedPreferences from anywhere in your application use:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPrefences(context);
To set a value in the preferences, you need to call the editor for those preferences, then set the value for a key and finally commit the result. It can all be done in a single line:
prefs.edit().putString("myKey","myValue").commit();
This would store the string value myValue on a key named myKey and it will be accessible (after you commit) to any class if it has the application's context when it calls getDefaultSharedPreferences.
To retrieve the stored value you specify the key and a fallback value in case there is no preference set with that key:
prefs.getString("myKey","oops no value found");
I have Listpreferences in my app. They don't appear to be setting to their defaults right after installation - they appear to be null. I'm trying to figure out why my default preferences are not being set right after installation. In my main code I have:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
InUnits = sp.getString("List1", "defValue");
InAngs = sp.getString("List2", "defValue");
OutUnits = sp.getString("List3", "defValue");
OutAngs = sp.getString("List4", "defValue");
Right after the above code executes, each variable contains "defValue" instead of the actual values I have assigned in my ListPreference below.
My preference xml file is called, "settings.xml". Here's what one of the ListPreferences there looks like:
<ListPreference
android:key="List1"
android:title="Input: Alph"
android:summary="Choose Alph or Ralph"
android:entries="#array/inputAlph"
android:entryValues="#array/input_Alph_codes"
android:dialogTitle="Input Alph"
android:defaultValue="ININ"/>
Here's what some of my strings.xml file looks like:
<string-array name="inputUnits">
<item>Alph</item>
<item>Ralph</item>
</string-array>
<string-array name="input_Alph_codes">
<item>ININ</item>
<item>INMM</item>
</string-array>
When I go to menu, and then settings, I can see my defaults checked (radiobuttoned). Then when I go back from the settings menu to my main screen - all is well - for life! ...then each var above is assigned the proper default value.
This only happens when I first install my app on the phone. After I go to the settings screen once and then right out of it, the app is fine and accepts any setting changes.
By the way, as you can see, "List1" is the android:key within a file called settings.xml in my res/xml folder.
They don't appear to be setting to
their defaults right after
installation - they appear to be null.
That's what's supposed to happen.
I'm trying to figure out why my
default preferences are not being set
right after installation.
They're not supposed to be. The preference XML you have listed there is only used for populating a PreferenceActivity, nothing more. Until the user opens the PreferenceActivity, the preferences will be null, and the defaults you supply to the SharedPreferences getters will be returned.
UPDATE
You can use setDefaultValues() on PreferenceManager to assign the defaults from your preference XML to a SharedPreferences. However, be careful of the timing -- this will do disk I/O, and therefore ideally is performed on a background thread.
Set the default values to SharedPreferences from your preference XML.
PreferenceManager.setDefaultValues(Context context, int resourceId, boolean readAgain)
PreferenceManager.setDefaultValues
You can specify a default value like this
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
pref.getString("thePrefKey", "theDefaultValue");
The android:defaultValue="..." in the "layout" settings.xml is only a visual help for user