Search code examples
androidxmlpreferences

Set a check box to false in Preference Activity


I am trying to implement user preferences. A problem I have is that if one of my check boxes is ticked I want to set the other to false. I know about dependency and from what I have seen it doesn't change the value. Just disables you from changing the preference. What can I do to change a checkbox to false if another is true? This is my xml:

   <PreferenceCategory android:title="Launch Settings">
    <CheckBoxPreference
            android:key="enable_nearest_station"
            android:title="Nearest station"
            android:defaultValue="false"
            android:summary="Will launch your nearest station on startup" 
            android:disableDependentsState="true"/>
<CheckBoxPreference
    android:key="enable_default_station"
    android:title="Default station"
    android:defaultValue="false"
    android:dependency="enable_nearest_station"
    android:summary="Allows you to choose a default to launch with."
    />

       />

</PreferenceCategory>

Thanks in advance.


Solution

  • Use the set setOnPreferenceChangeListener(Preference.OnPreferenceChangeListener onPreferenceChangeListener) to your CheckBoxPreference and code to set false other CheckBoxPreference.