Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 428
brybam Avatar asked Aug 28 '10 18:08

brybam


People also ask

What is PreferenceFragmentCompat?

A PreferenceFragmentCompat is the entry point to using the Preference library. This Fragment displays a hierarchy of Preference objects to the user. It also handles persisting values to the device. To retrieve an instance of android.

What is preference activity Android?

PreferencesActivity is a way to easily create preference screens such as those in Android itself, just look under Settings . These can be used inside applications to easily save preferences to SharedPreferences and then easily access these from within your app. See this page for more information on PreferenceActivity.

How do I get to preferences in Android Studio?

From the menu bar, click File > Settings (on macOS, click Android Studio > Preferences).


2 Answers

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.

like image 166
brybam Avatar answered Oct 12 '22 01:10

brybam


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));     } } 
like image 24
Steve Waring Avatar answered Oct 12 '22 02:10

Steve Waring