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

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

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

edit: maybe the problem is not in android:dependency , maybe there is an xml attribute that I can add to disable this default option, and then android:dependency switch it to the opposite way as I want.

edit again: I tried to set android:enabled="false" in the preference, and it disables it as I want, but even if it depends on another preference, it did not enable it, as I hoped

+59
java android xml dependencies preferences
Aug 28 '10 at 18:19
source share
5 answers

Actually I found this on my own and decided that I just posted it here to help anyone who might have the same problem:

 android:disableDependentsState="true" 

Put this in control preference.

+113
Aug 28 '10 at 18:35
source share
β€” -

Dmitry Zarezenko asked that if you want some dependencies to be included when the preference on which they depend is true, and some should be included if this preference is wrong.

Use the method described above to establish all dependent preferences of the same type (which ever have the largest number). Then (with a class that implements OnSharedPreferenceChangeListener), there is such a code in the Preference and / or Preferred Fragment section:

 @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)); } } 
+8
May 15 '13 at 14:18
source share

This is my sample code for this code, 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) { } 
+1
Dec 15 '11 at 16:12
source share

I need to change the value of the dependent preference, so I post my code below if someone 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); } 
0
Mar 21 '14 at 8:26
source share

Make your PreferenceActivity implement

SharedPreferences.OnSharedPreferenceChangeListener

declare in PreferenceActivity :

SharedPreferences prefs;

initialize in onCreate :

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

and register with the general preference change listener

 prefs.registerOnSharedPreferenceChangeListener(this); 

do what Steve said in the onResume and onPause .

implementation of the 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 settings in PreferenceActivity . Call and record. If both boxes are unchecked, both can be checked, but when the user checks one of them, the other becomes grayed out. When the user cancels the selected preferences, the user can check another.

In both cases, other settings may be dependent, which can be worked out with the android:dependancy in the XML file.

0
Jan 25 '15 at 15:57
source share



All Articles