I am trying to accomplish such a thing: when I check CheckBoxPreference 'A', another preference ('B') is displayed below A, when I uncheck "A", preference "B" is hidden ...
So generally speaking, it should work just like a dependency, but not only enable / disable preference B, but also hide it.
Here is what I came up with:
prefA = (CheckBoxPreference)findPreference("preference_A");
prefA.setChecked(false);
prefB = findPreference("preference_B");
category.removePreference(prefB);
prefA.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
boolean switchedOn = (Boolean)newValue;
if (switchedOn)
{
Log.d("pref_test", "prefA checked");
category.addPreference(prefB);
}
else
{
Log.d("pref_test", "prefA UNchecked");
prefB = findPreference("preference_B");
category.removePreference(prefB);
}
return switchedOn;
}
});
prefA and prefB were previously defined as fields of the PreferenceFragment class.
The problem is that it works just fine in 2 clicks, and my logs say:
prefA checked
prefA UNchecked
prefA UNchecked
As if he had called onPreferenceChangeListener twice to uncheck it (obviously .removePreference (prefB) method returned null).
Any idea to solve the problem?