Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Get Keys from preferences.xml

I have a PreferencesActivity that shows a preferences.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 different Activity, only from the PreferenceActivity.

Does anybody have a solution how to get this work?

like image 452
liquid Avatar asked Mar 09 '11 13:03

liquid


3 Answers

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.

like image 111
rekaszeru Avatar answered Oct 04 '22 11:10

rekaszeru


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.

like image 23
Ridcully Avatar answered Oct 04 '22 10:10

Ridcully


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

like image 30
Victor Ionescu Avatar answered Oct 04 '22 11:10

Victor Ionescu