androidpreferenceslistpreferencecheckboxpreference

multiple dependency in checkboxpreference android or listpreference's dependency


For example: i have three checkboxes in my preference screen and there is 3 different listpreference(A,B,C) depended on each checkbox. i want to make the user select only one checkbox at a time. How do i achieve this?

  1. There is no radio button in preference screen

  2. I can not use Listpreference , if i can use it

      ListPreference
        android:key="livewallpaper_testpattern"
        android:title="@string/livewallpaper_settings_title"
        android:summary="@string/livewallpaper_settings_summary"
        android:entries="@array/livewallpaper_testpattern_names"
        android:entryValues="@array/livewallpaper_testpattern_prefix"
    

Array of this Listprefrence is "blue"," red", "white"

if it is blue ListPreference A depends on blue

if it is red ListPreference B depends on red

if it is white ListPreference C depends on white

How can i do this?

i searched 3-4 pages in google and here almost everything about these but i could not find any answer.

Best Regards,

Thanks in advance..


Solution

  • You can override onSharedPreferenceChanged in your PreferenceActivity class and enable/disable appropriated Preferences programmatically:

    public class MyPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {
        ...
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            if (key.equals("livewallpaper_testpattern")) {
                if (/* check if livewallpaper_testpattern equals to blue */) {
                    findPreference("ListPreferenceKey_A").setEnabled(true);
                    findPreference("ListPreferenceKey_B").setEnabled(false);
                    findPreference("ListPreferenceKey_C").setEnabled(false);
                } else if (/* check if livewallpaper_testpattern equals to red */) {
                    // enable B, disable A & C
                } else if (/* check id livewallpaper_testpattern equals to white */) {
                    // enable C, disable A & B
                }
            }
        }