Android Get Keys from preferences.xml
Solution 1
You don't need a PreferenceActivity
to accomplish this.
To access the preferences, that are used in your PreferenceActivity, you should use
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(getBaseContext());
If your list of data comes from the server, than you can use a ListActivity / ExpandableListActivity / any custom activity to visualize it, but this way you need to write the handlers that change the preferences.
The common way to do this would be:
private void saveStringPreference(final String key, final String value)
{
SharedPreferences.Editor editor = prefs.edit();
editor.putString(key, value);
editor.commit();
}
If you need, you should create similar wrappers to deal with int, boolean, etc. values.
Solution 2
I use
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(getBaseContext());
And then I can access the preferences via prefs.get...()
, e.g. prefs.getString(key)
.
Have you tried this?
Edit: Just checked - prefs.getAll()
works as expected and returns a Map
with all preferences.
Solution 3
I had this problem too. This is a classic RTFM, sadly. You should put the line below in your MainActivity's onCreate() method:
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
This info can be found in the paragraph "Setting Default Values" of http://developer.android.com/guide/topics/ui/settings.html
liquid
Updated on July 06, 2022Comments
-
liquid almost 2 years
I have a
PreferencesActivity
that shows apreferences.xml
with checkboxes.preferences.xml
:<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="Keywords"> <CheckBoxPreference android:key="Essen" android:title="Essen" android:selectable="true" android:enabled="true" android:persistent="false"> </CheckBoxPreference> <CheckBoxPreference android:key="Kleidung" android:title="Kleidung" android:selectable="true" android:enabled="true" android:persistent="false"> </CheckBoxPreference> </PreferenceCategory> </PreferenceScreen>
PreferencesActivity:
public class PreferencesViewController extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); } }
Now in a different
ListActivity
I want to show all Keys/Titles from the checked checkboxes.I try to access the Preferences with
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
or
SharedPreferences prefs = getSharedPreferences("mypackage_preferences", 0);
But both dont really work.
When I call
prefs.getAll().size()
the result is 0.I can access the Keys/Title with
getPreferenceScreen().getPreference(i).…
but it doesn't work from a differentActivity
, only from thePreferenceActivity
.Does anybody have a solution how to get this work?
-
liquid over 13 yearsThe Problem is that I want to update these preferences from Internet/Server. So i dont know the keys for every checkbox. So the Preferences should be shown in an Activity/View, the User can check/uncheck them. I have to read the checked preferences and the preferences should be updated by a server… maybe the PreferenceActivity is not the right solution?! :-/
-
EGHDK over 12 yearsThank you. I have been searching for this answer for about an hour. I had a preferences.xml and preferences.java but I wanted to use another screen to set the values. Thanks again.
-
Mr_and_Mrs_D over 10 yearsWhy
getBaseContext()
? -
Denny over 6 yearsI guess we'll never know