Search code examples
androidandroid-preferences

Android CheckBoxPreference: how to disable and enable other preferences on preference change


I have CheckBoxPreference and 2 others: one is Edit Test Pref. and another is ListBox Pref. How I can enable list box pref and disable edit text pref. when CheckBoxPreference is turned on?


Solution

  • Seems it's duplicate of this question

    You can override public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) and when it get called with key.equals(<Your key for setting>) do something like:

    boolean isEnabled = sharedPreferences.getBoolean(key, true);
    getPreferenceScreen().findPreference("list box preference key").setEnabled(isEnabled);
    getPreferenceScreen().findPreference("list box preference key").setEnabled(!isEnabled);
    

    Also, do the same in onCreate() to have Your preferences screen proper initial setup.