Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the Category of an Android Preference?

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?

like image 846
gregm Avatar asked May 30 '11 13:05

gregm


1 Answers

There is no getCategory() method but you could use this following workaround: If you don't use dependency attribute for your Preference you could set your Preference as dependent from PreferenceCategory and then use getDependency() method. For example:

<PreferenceCategory 
    android:title="MyPrefCat" 
    android:key="category" 
    android:selectable="false">  
    <CheckBoxPreference 
            android:key="chkbox" 
            android:dependency="category" />
</PreferenceCategory>

In the PreferenceActivity you could use now:

CheckBoxPreference currentPref = (CheckBoxPreference) findPreference("chkbox");
String prefCatKey = currentPref.getDependency();
PreferenceCategory catPref = (PreferenceCategory) findPreference(prefCatKey);
// Access PreferenceCategory's attributes such as its title:
String prefCatTitle = catPref.getTitle().toString();
like image 172
Marcin S. Avatar answered Dec 16 '22 18:12

Marcin S.