Shmuel
Shmuel

Reputation: 3916

Android Settings - inverse dependency

I have a primary Boolean setting that when true would make one child active, but when it is false child a would be disabled and child B would be active.

ie;

if (ParentSetting == true) activate childSettingB elseIf(ParentSetting == false) activate childSettingB

I know how to have dependency in the xml tags, but is it possible to have an inverse relationship towards a specific child setting?

thank you

ps. I want to do it in XML if possible...

Upvotes: 3

Views: 2998

Answers (2)

Leebeedev
Leebeedev

Reputation: 2146

Use the android:disableDependentsState="true" in the CheckBoxPreference

For Example:

<CheckBoxPreference
        android:key="cb_key"
        android:title="@string/stop_service"
        android:summaryOn="On
        android:summaryOff="Off"
        android:defaultValue="false"
        android:disableDependentsState="true" />
   <Preference
        android:key="pref_key"
        android:title="pref_title"
        android:summary="pref_summary"
        android:dependency="cb_key" />

this way the Preference will be disabled when the CheckBoxPreference is ON "true" and vice versa.

Upvotes: 11

Aswin Rajendiran
Aswin Rajendiran

Reputation: 3409

There is no way to achieve this purely in xml.

Using android:disableDependentsState="true" on the parent (preference which is being depended on) will allow you to inverse the dependency but it will both the child settings. This is not what you want. disableDependentsState

Only way is to do it is programmatically. You probably know how to do that already.

SwitchPreference dependee = (SwitchPreference) findPreference("dependee");
dependent2.setEnabled(!dependee.isChecked());
dependee.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
    public boolean onPreferenceChange(Preference preference, Object newValue){
            boolean isEnabled = ((Boolean) newValue).booleanValue();
            dependent2.setEnabled(!isEnabled);
            return true;
        }
    });

Upvotes: 6

Related Questions