I need to remove a PreferenceCategory programmatically. I could remove the individual preferences with the following code but I need to remove (disable) whole PreferenceCategory as well.
PreferenceScreen preferenceScreen = getPreferenceScreen();
EditTextPreference etp = (EditTextPreference) preferenceScreen.findPreference("pref22");
((PreferenceGroup) findPreference("prefcat")).removePreference(etp);
Edit: Here's the working code for a PreferenceCategory "prefcat" and a child preference "pref22":
PreferenceScreen preferenceScreen = getPreferenceScreen();
EditTextPreference etp = (EditTextPreference) preferenceScreen.findPreference("pref22");
PreferenceGroup preferenceGroup = (PreferenceGroup) findPreference("prefcat");
if (preferenceGroup != null) {
preferenceGroup.removePreference(etp);
preferenceScreen.removePreference(preferenceGroup);
}
Don't load the PreferenceCategory
in the first place.
If you are defining your preferences in Java, don't create the PreferenceCategory
.
If you are defining your preferences in XML, use three XML files:
In situations where you want the category, load all three XML files. In situations where you do not want the category, load only the first and third XML files.