How to do opposite of of preference attribute android:dependency?

11,948

Solution 1

Actually found it on my own and figured I'd just post it here to help anyone that might have this same issue:

android:disableDependentsState="true"

Put that in the controlling preference.

Solution 2

Dmytro Zarezenko asked what if you wanted some dependencies to be enabled when the preference on which they depend is true and some to be enabled when that preference is false.

Use the method described above to set the all the dependant preferences of one type (which ever have the greater number). Then (with the class having implements OnSharedPreferenceChangeListener) have code like this in the Preference Activity and/or Preference Fragment:

@Override
public void onResume()
{
    super.onResume();
    sharedPreferences.registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onPause()
{
    super.onPause();
    sharedPreferences.unregisterOnSharedPreferenceChangeListener(this);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
{
    if (key.equals("pref_that_they_depend-upon")
    {
        // Iterate over the preferences that need to be enabled or disabled,
        // lets say there is just one called the_awkward_one.
        Preference preference = findPreference("the_awkward_one");
        // Or preference.setEnabled(! sharedPreferences.getBoolean(("pref_that_they_depend-upon", defaultValue));
        preference.setEnabled(sharedPreferences.getBoolean(("pref_that_they_depend-upon", defaultValue));
    }
}

Solution 3

This is my code sample for doing this from code and not XML.

  String eitherKey = "either";
  String orKey = "or";
  CheckBoxPreference either = new CheckBoxPreference(this);
  either.setKey(eitherKey);
  either.setTitle("Either");
  either.setSummary("It is either one or");
  either.setDefaultValue(false);
  either.setDisableDependentsState(true);
  inlinePrefCat.addPreference(either);

  try
  {
     //Crossfade Time
     CheckBoxPreference or = new CheckBoxPreference(this);
     or.setKey(orKey);
     or.setTitle("Or");
     or.setSummary("the other");
     inlinePrefCat.addPreference(or);
     or.setDependency(eitherKey);
  }
  catch (Exception e)
  {
  }
Share:
11,948
brybam
Author by

brybam

Brinkbit.com

Updated on June 15, 2022

Comments

  • brybam
    brybam about 2 years

    Is there XML attribute that does the exact opposite of android:dependency?

    What I would like the dependent preference to be enabled when the other is NOT checked and disabled when it IS checked.

    edit: maybe the issue isn't with android:dependency maybe there is an xml attribute that I can add to make the default for that preference disabled and then android:dependency will toggle it the opposite way like i want.

    edit again: I tried setting android:enabled="false" in the preference and it disables it like i want but even with it being dependent on the other preference it didn't enable it like i had hoped

  • Patrick Boos
    Patrick Boos over 13 years
    confirmed.. add that line to the Preference that you put in android:dependency :)
  • BoD
    BoD over 12 years
    To be even clearer: put that line on the Preference on which your Preference depends. The doc says: "The state (true for on, or false for off) that causes dependents to be disabled".
  • Dmytro Zarezenko
    Dmytro Zarezenko over 11 years
    But if I want enable some prefs and disable some prefs depends on one checkbox pref?
  • ToolmakerSteve
    ToolmakerSteve almost 10 years
    IMHO, Steve Waring's solution is preferable, because it works regardless of what causes the preference value to change. That is, if you set "key_a" in code, Waring's solution will take effect, whereas the one here will not (because there is no "click").
  • mehmet
    mehmet over 6 years
    perfect! for more obvious "Put that in the controlling preference" mean that, add android:disableDependentsState="true" code in SwitchPreference (Master) which is affect others (Slaves)