Search code examples
androidandroid-preferences

Get all Preferences from PreferenceFragment


In my Android application I have a PreferenceFragment which is build from a XML resource file. After the XML is loaded I would like to call a method for each Preference in the Fragment. My problem is that I haven't found a method which gives me all Preferences from the current PreferenceFragment.

I know that I can find a preference with the findPreference(...) method but I would like to get an iterator of all Preferences. Have a missed a method or aren't we able to get all Preferences?


Solution

  • I found a method to do this with the code below:

    PreferenceScreen prefScreen = getPreferenceScreen();
    int prefCount = prefScreen.getPreferenceCount();
    
    for(int i=0; i < prefCount; i++) {
        Preference pref = prefScreen.getPreference(i);
        // do something with the Preference
    }