Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CheckBoxPreference and dependency

Tags:

android

I've a checkbox and a ListPreference, I'd like disable/enable ListPreference by checkbox. I read a lot and I found is possible only using java (and not by xml). Is it correct? Now, after read the value of "checkboxPref" (boolean true/false) I don't how do.

SharedPreferences prefs3 = PreferenceManager.getDefaultSharedPreferences(this);
      listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
          public void onSharedPreferenceChanged(SharedPreferences prefs, String listpref) {       

              CheckboxPreference = prefs.getBoolean("checkboxPref", true);
              }   };

      prefs3.registerOnSharedPreferenceChangeListener(listener);

preferences.xml:

<CheckBoxPreference
                        android:title="Notifify"
                        android:defaultValue="true"
                        android:key="checkboxPref" />
            <ListPreference
         android:entries="@array/numberOptions"
         android:entryValues="@array/numberValues"
         android:key="number"
         android:title="Number" />
like image 399
Pol Hallen Avatar asked Dec 31 '12 12:12

Pol Hallen


1 Answers

Add

android:dependency="checkboxPref"

To the ListPreference XML

Result:

<ListPreference
         android:entries="@array/numberOptions"
         android:entryValues="@array/numberValues"
         android:key="number"
         android:title="Number"
         android:dependency="checkboxPref"
 />
like image 188
Nicklas Gnejs Eriksson Avatar answered Oct 01 '22 16:10

Nicklas Gnejs Eriksson