Alex Rivera | Logout

How do I get the Category of an Android Preference?

Asked 2011-05-30T13:59:33.170
12

How do I get the PreferenceCategory of a Preference? PreferenceManager has a findPreference method but a Preference does not have a getCategory method.

Is there a way to get the PreferenceCategory of a Preference from it's key name?

Edit
Report

2 Answers

3

Are you trying to do this from within the PreferenceActivity or elsewhere? If from there, try this out. It should loop through your categories and find the right one. Just make sure you update the catKeys array to include the categories you want to search each time you change your xml.

String catKeys = {"catOne","catTwo","catThree","catFour"};

String getCategoryKey(String prefKey)
{
    PreferenceCategory curCat;
    Preference test;
    for(String catKey : catKeys)
    {
        curCat = (PreferenceCategory)findPreference(catKey);
        if(curCat == null)
            continue;
        test = curCat.findPreference(prefKey);
        if(test != null)
            return catKey;
    }
}
answered 2011-05-30T17:18:39.557
0

I don't see a getCategory or getGroup neither in Preference.

But, you can use the methods of PreferenceCategory inherited from http://developer.android.com/reference/android/preference/PreferenceGroup.html

getPreference(int index)

int getPreferenceCount()

That's a long way indeed for what you want to do, but it looks like you should rebuild the preference menu indeed.

Other alternative would be to saxp your xml prefrence file.

Regards, Stéphane

answered 2011-05-30T14:08:20.090

Your Answer