67

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

brybam
  • 5,009
  • 12
  • 51
  • 93

5 Answers5

148

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.

Mohit Deshpande
  • 53,877
  • 76
  • 193
  • 251
brybam
  • 5,009
  • 12
  • 51
  • 93
  • 1
    confirmed.. add that line to the Preference that you put in android:dependency :) – Patrick Boos Feb 22 '11 at 01:44
  • 7
    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". – BoD Nov 30 '11 at 10:50
  • 28
    But if I want enable some prefs and disable some prefs depends on one checkbox pref? – Dmytro Zarezenko Nov 08 '12 at 14:22
  • 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) – mehmet Dec 13 '17 at 13:50
  • 1
    @DmytroZarezenko popping in a decade later to answer your comment, since I had this exact problem just now: Say you have master preference A and slave prefs B and C, where B and C should show opposite behaviour depending on A. If B and C is set to depend on A this cannot be achieved. But instead, you can let C depend on B while B depends on A. Then B and C can be set to behave opposite of eachother buy using the correct "disableDependentsState" setting on prefs A and B. – Leknesh Feb 15 '23 at 08:10
9

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));
    }
}
Steve Waring
  • 2,882
  • 2
  • 32
  • 37
1

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)
  {
  }
Justin
  • 874
  • 8
  • 15
0

I need to change value of dependent preference, so i post my code below, if anyone wants to do this:

@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
    if(preference.getKey().equals("key_a")) {
        ((CheckBoxPreference)findPreference("key_b").setChecked(false);
    }
    return super.onPreferenceTreeClick(preferenceScreen, preference);
}
davidm
  • 23
  • 6
  • 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"). – ToolmakerSteve Sep 18 '14 at 00:40
0

Make your PreferenceActivity implement

SharedPreferences.OnSharedPreferenceChangeListener

declare in PreferenceActivity:

SharedPreferences prefs;

initialize in onCreate:

SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(this); prefs = sPrefs;

and register on shared preference change listener

prefs.registerOnSharedPreferenceChangeListener(this);

do the same as Steve said in onResume and onPause methods.

implementation of onSharedPreferenceChanged listener:

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    Log.d("SettingsActivity","onSharedPreferenceChanged LISTENER FIRED");
    if (key.equals(getString(R.string.key_call))) {
        //if call true
        if (sharedPreferences.getBoolean(getString(R.string.key_call), false)) {
            Preference preference = findPreference(getString(R.string.key_record));
            preference.setEnabled(false);
        } else { // if call false
            Preference preference = findPreference(getString(R.string.key_record));
            preference.setEnabled(true);
        }
    }
    if (key.equals(getString(R.string.key_record))) {
        //if record true
        if (sharedPreferences.getBoolean(getString(R.string.key_record), false)) {
            Preference preference = findPreference(getString(R.string.key_call));
            preference.setEnabled(false);
        } else { // if record false
            Preference preference = findPreference(getString(R.string.key_call));
            preference.setEnabled(true);
        }
    }
}

In this case, I have 2 mutually exclusive Preferences in PreferenceActivity. Call and Record. When both are unchecked, both can be checked, but as user checks one of them, the other becomes disabled (greyed out). As user unchecks the checked preference, the user can check the other one.

On both of them other preferences can depend and that can be worked out with android:dependancy attribute in XML file.

Dhanuka
  • 2,826
  • 5
  • 27
  • 38