Search code examples
androidandroid-preferences

How to enable a preference in my android application when other preference is disabled?


I have used PreferenceActivity to have preference in my android application. I want one preference say "pref 2" to be enabled when other preference say "pref 1" is NOT checked and "pref 2" to be disabled when "pref 1" is checked. i.e. exactly opposite of the android:dependancy attribute.

How can I do that?


Solution

  • I don't think there's any out-of-the-box solution for it, i.e. an inverted dependancy attribute. But there's always the click-listener:

    preference1.setOnPreferenceClickListener(pref1_click);
    
    ....
    
    private OnPreferenceClickListener pref1_click = new OnPreferenceClickListener() {
        public boolean onPreferenceClick(Preference preference) {
            preference2.setEnabled(!preference1.isChecked());
            return true;
        }
    }