I have a bit of confusion about setting default values of shared preferences... for example, I have a PreferenceActivity with two or more PreferenceFragment and each of the preference fragmens has its own *preference_fragmentX.xml* file used to build the preference view. When I set the default values with
PreferenceManager.setDefaultValues(getActivity(), R.xml.?????, false);
Which XML should I specify? Should I build another XML preference file containing all the preferences of the *preference_fragmentX.xml* files with the default values and use this one in setDefaultValues? For example, I may build an app that registers for a broadcast event and I'm not sure the user opens the preference activity before the broadcast event is fired for the first time, so I'd like to initialize default preferences in the broadcast event, or in another activity started by it.. so, how do I initialize all the preferences of all the fragment panes?
It works if I build a default_preferences.xml file which contains all the preference keys defined in all the preference fragments XML files (I specify only the a PreferenceScreen root tag and all the preference tags under it, without specifying all the other tags like PreferenceCategory.. furthermore for each preference I specify only the key and the default vale attributes), but I don't know if it is the correct way to do this task, since I must replicate part of the other XML files:
preferences_fragment1.xml:
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="#string/my_preference_category">
<RingtonePreference
android:key="preference_ringtone"
android:ringtoneType="alarm|notification"
android:showSilent="true"
android:summary="#string/preference_ringtone_summary"
android:title="#string/preference_ringtone_title" />
<ListPreference
android:key="preference_list"
android:entries="#array/list_items"
android:entryValues="#array/list_values"
android:summary="#string/preference_list_summary"
android:title="#string/preference_list_title" />
</PreferenceCategory>
</PreferenceScreen>
default_preference.xml:
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<RingtonePreference
android:defaultValue="content://settings/system/notification_sound"
android:key="preference_ringtone"/>
<ListPreference
android:defaultValue="5"
android:key="preference_list"/>
</PreferenceScreen>
Related
I have the following code in my xml resource for my settings menu:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:defaultValue="top_rated"
android:dialogTitle="Search for:"
android:entries="#array/SettingEntries"
android:entryValues="#array/SettingEntriesValue"
android:key="pref_sync"
android:summary="%s"
android:title="Show" />
</PreferenceScreen>
When my app loads the first time, i dont want it to load the stored value from the previous run, but select the first value on the list.
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
sharedPrefs.edit().putString("pref_sync", "top_rated");
I tried to change the value in SharedPreferences when the app gets created but that didn't work. Any advice on how to solve the matter?
In my preference xml I have:
<EditTextPreference
android:title="Location"
android:key="#string/pref_location_key"
android:defaultValue="689558"
android:inputType="text"
android:singleLine="true" />
So the default value for this preference supposed to be "689558", but when I run my app it looks like this:
In short, I have mysterious "94043" value instead of "689558".
Where I could make mistake?
Didn't you set your preference before? Try to delete the app data from settings/applications and reinstall your app.
If you set the preference once it will remain the same independently from restart or reinstall.
I have a CheckBoxPreference defined as follows:
<CheckBoxPreference
android:defaultValue="true"
android:key="prefVisible"
android:summary="#string/pref_visible_summary"
android:title="#string/pref_visible" >
</CheckBoxPreference>
My application uses this preference to control the visibility of a view. When I first start my application (on a new wiped emulator) the view is not shown. However, when the I go to the preferences screen (activity) the checkbox is shown as checked.
Does this mean that the defaultValue attribute is not actually setting the preference but rather it's just setting the value of the checkbox if there is no underlying data (as would be the case on a brand new install). And does this also mean that the preference is set only after the user enters/exits the preferences screen (activity) for the first time, otherwise it's undefined?
Note that in order to get my app to work the way I intended it to work I relied on the default value argument to the preferences getter method as follows:
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean isVisible = sharedPrefs.getBoolean("prefVisible", true); // default = true
This leaves me a bit confused as to why there are 2 ways to control the default value of a preference: defining it in the Xml or providing the default value in the getBoolean method.
No the preferences can be set if you call PreferenceManager.setDefaultValues. So if you call this when the first time the app is launched then your view will be shown.
You can read more at http://developer.android.com/guide/topics/ui/settings.html
I believe I am correctly initializing preferences from XML. My Preferences Screen also works properly and reflects the correct user selected settings.
However, upon first invocation of that Preferences Screen, none of the settings are checked (checkbox) or selected (list). This, of course, confuses the user as it doesn't reflect the current (default/initial) value.
Since all I do to invoke the Preferences Screen is
startActivity(new Intent(this, EditPreferences.class));
And my EditPreferences class only contains:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.usersettings);
}
I am not sure where or how to tell it to pre-initialize the visual display with the default setting.
I have this hunch that all I am missing is a single line somewhere, but I don't know where: XML file? override a method in EditPreferences? Other?
Can't you define the default value in the XML itself?
<CheckBoxPreference ...
android:defaultValue="true"
... />
You can specify a default value on a preference (in your xml layout for example):
<EditTextPreference android:defaultValue="whatever" />
I have a preferences page which is defined by XML - including some default values. I use a PreferenceActivity to display and handle this page. Whenever I use this page to set the preferences the preference file on the file system is updated properly - I can see this via adb.
However, whenever I go back to the settings page after have changed some of the settings, it's the defaults that are shown. Worse than that, if I press back without changing any settings, it then sets the, all back to the default.
Any ideas on how I can get the prefs to actually show the current settings?
My PreferenceActivityis created thus:
#Override
protected void onCreate(Bundle savedInstanceState) {
PreferenceManager.getDefaultSharedPreferences(this)
.registerOnSharedPreferenceChangeListener(this);
setDefaults(this);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
The XML looks like this:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:id="#+id/numberOfYearsList"
android:key="numberOfYears"
android:title="Number of Years to Read the Bible"
android:summary="How many years would you like to take to read through the reading plan?"
android:entries="#array/numberOfYears"
android:entryValues="#array/numberOfYears"
android:dialogTitle="How Many Years?"
android:defaultValue="1"
/>
<CheckBoxPreference android:key="ignoreDates"
android:id="#+id/ignoreDatesCheckbox"
android:title="Ignore Dates"
android:summary="Would you like to use the dates in the plan?"
android:defaultValue="false"
/>
</PreferenceScreen>
Nevermind - I did a mistake. As you can see from above, when creating the Preference Activity I'm making a call to a method that sets all the prefs to defaults... Duh!