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?
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With