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!
Related
I have a Preference Activity
But, whenever I call it, the preferences are empty. No title or summaries are visible. You can click on the preference and it will highlight but there is no text. I have tried adding an EditTextPreference and in that case an edit text will pop up on click but there is no text.
Activity:
public class Preferences extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
android:title="SomePref"
android:summary="Somethingsomething"
/>
</PreferenceScreen>
Thanks
You may try to add this inside PreferenceScreen and see what happens:
<PreferenceCategory
android:title="Preference Category">
<CheckBoxPreference
android:defaultValue="True"
android:key="checkbox_key"
android:title="CheckBox Title"
android:summaryOn="Checkbox is ticked"
android:summaryOff="Checkbox is not ticked" />
</PreferenceCategory>
I figured it out. I'm using the ActionBarSherlock library and I was extending the regular PreferencesActivity when it should have been Sherlock*PreferencesActivity
Everything works great now.
Always, throughout the app save your preferces through getApplicationContext(), if you use this as context it will save with activity context and cannot retreived all the time
I am using PreferenceScreen to set some user preferences in my Android app. It works perfectly for several ListPreference and CheckboxPreference items, however I cannot get RingtonePreference to work properly. The appropriate ringtone dialog displays, and can be selected, but the selection is never saved.
My app only plays the default sound no matter what I select. Whenever I re-open the ringtone dialog (either immediately after making a selection, or after exiting the app and coming back in) it always just has the default item selected. I have a field to display the preference value, and it always shows the default sound is selected, even after changing it on the preferences screen. I also confirmed that the appropriate xml file on my device (in /data/data/myapp/shared_prefs) is not changing when I monitor it with DDMS. If I change other items (such as a CheckboxPreference), I see the shared_prefs file change realtime. I have stripped my PreferenceScreen to the bare minimum and it still behaves the same. I tried changing key names, defaults, and clearing app data on my phone.. nothing seems to work.
I did find 2 similar questions already posted on SO (here and here), but they are several months old, with no answers and/or the person asking the question gave up and tried another method. I'd like to figure out why it does not appear to work as designed.. or at least, find a suitable method to get the same thing done.
The relevant portions of my code are below.. thanks in advance!
/res/xml/preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<RingtonePreference
android:key="alertSound"
android:ringtoneType="notification"
android:summary="Select audio notification sound"
android:title="Alert Sound" >
</RingtonePreference>
</PreferenceScreen>
/src/myapp/EditPrefsActivity.java:
public class EditPrefsActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
settings = PreferenceManager.getDefaultSharedPreferences(this);
addPreferencesFromResource(R.xml.preferences);
}
}
I just found the solution! The answer was recently posted in a similar question: https://stackoverflow.com/a/15887357/1992342
Removing android:launchMode="singleInstance" from the EditPrefsActivity <activity> entry in the manifest solves the problem. Apparently it is a [little known] Android bug.
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>
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'm currently writing a live wallpaper for Android and it has a PreferenceScreen which currently contains only one preference - a DialogPreference to set various properties of animation.
User workflow to configure it currently looks like this:
Settings... => (shows the preferences list with only one title ) Animation speed => MyDialogPreference
What I want is to make the workflow like this:
Settings... => MyDialogPreference
I.e. I'm looking for a way to skip showing that preferences list with only one item and to show that dialog right away.
But it seems that PreferenceActivity requests itself to have PreferenceScreen as a root element of preference hierarchy. So... is it even possible to do what i want? :)
Code references:
Activity code:
public class ForestLakePreferences extends PreferenceActivity
{
protected void onCreate(Bundle savedState)
{
super.onCreate(savedState);
getPreferenceManager().setSharedPreferencesName(
ForestLakeWallpaper.PREFS_NAME);
addPreferencesFromResource(R.xml.preferences);
}
}
Prefs resource:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="lake_preferences">
<DurationEditDialog
android:title="#string/prefs_duration_title"
android:dialogTitle="#string/configure_durations_dlg_title"
android:dialogLayout="#xml/set_durations_layout" />
</PreferenceScreen>
It turned out this can't be done in that way.
But i've found a workaround: i made my activity not a PreferencesActivity, but a custom one and made it look like dialog by placing the following in AndroidManifest.xml
<activity android:theme="#android:style/Theme.Dialog" .... />
A DialogPreference has a showDialog(Bundle state) method, try calling it. I am not sure if you will have to give it anything else like the Preferences or anything.