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
This is my code sample for doing this from code and not XML.
Actually found it on my own and figured I'd just post it here to help anyone that might have this same issue:
Put that in the controlling preference.
I need to change value of dependent preference, so i post my code below, if anyone wants to do this:
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:
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
do the same as Steve said in
onResume
andonPause
methods.implementation of
onSharedPreferenceChanged
listener: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.