Nested preferences.xml Nested preferences.xml android android

Nested preferences.xml


Solution here it is to inflate both preference files from PreferencesActivity. For example:

    addPreferencesFromResource(R.xml.options);    addPreferencesFromResource(R.xml.additional_options);


The solution soul shows works. It can be expanded to only show preferences if you're the developer using an unsigned version of the app ;)

addPreferencesFromResource(R.xml.options);addPreferencesFromResource(R.xml.additional_options);if (BuildConfig.DEBUG) {    addPreferencesFromResource(R.xml.developer_options);}

I created a blog post regarding this issue and have a complete working code example available for download.http://androidfu.blogspot.com/2012/05/developer-debug-with-nested-preferences.html


To truly achieve the nesting effect you can use this technique to relocate the loaded preferences to a group already loaded.

PreferenceCategory notifications = (PreferenceCategory) getPreferenceScreen ().findPreference (PreferenceKey.pref_notifications.name ());addPreferencesFromResource (R.xml.pref_notifications, notifications);

Where the enhanced addPreferencesFromResource is defined as:

private void addPreferencesFromResource (int id, PreferenceGroup newParent) {    PreferenceScreen screen = getPreferenceScreen ();    int last = screen.getPreferenceCount ();    addPreferencesFromResource (id);    while (screen.getPreferenceCount () > last) {        Preference p = screen.getPreference (last);        screen.removePreference (p); // decreases the preference count        newParent.addPreference (p);    }}

It works for any PreferenceGroup such as PreferenceScreen and PreferenceCategory.