SwitchPreference and CheckBoxPreference in code

24,897

Solution 1

Maybe you could set an android:key attribute to both of your SwitchPreference and CheckBoxPreference xml, just like this:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <CheckBoxPreference
        android:key="pref_sync"
        android:title="@string/pref_sync"
        android:defaultValue="true" />
</PreferenceScreen>

and

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <SwitchPreference
        android:key="pref_sync"
        android:title="@string/pref_sync"
        android:defaultValue="true" />
</PreferenceScreen>

And then you can check if this key returns true or false on your code, something like this:

public class SettingActivity extends PreferenceActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    addPreferencesFromResource(R.xml.setting);
    PreferenceManager preferenceManager = getPreferenceManager();
    if (preferenceManager.getSharedPreferences().getBoolean("pref_sync", true)){
        // Your switch is on
    } else {
        // Your switch is off
    }
    ...
}

Hope this works for you.

Solution 2

In your Java code use TwoStatePreference, which is a parent class for both CheckBoxPreference and SwitchPreference. It has all the methods you are likely to need in your use case.

This is what the code sample you provided would look like:

public class SettingActivity extends PreferenceActivity {
    private TwoStatePreference enable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        addPreferencesFromResource(R.xml.setting);
        enable = (TwoStatePreference) findPreference(key_enable);
    }
    ...
}

Solution 3

Depending on what the code would want to do with the preference instance, it might be as simple as casting to Preference instead of a specific derived type, e.g.:

enable = (Preference)findPreference(key_enable);

which would then allow something like:

enable.setEnabled(true);

and eliminate the need to check API level in the code.

Share:
24,897
Chiman
Author by

Chiman

Updated on April 17, 2020

Comments

  • Chiman
    Chiman about 4 years

    I am now creating a preference page for my app

    After API-14, switchpreference is available. and i would like to use it to replace checkboxpreference on API14+ devices

    It is easy to use res/xml and res/xml-14 to get the correct xml resource

    However, in the coding part, it is not so convenient to switching the preference according to the API.

    public class SettingActivity extends PreferenceActivity {
        private CheckBoxPreference enable;
        private SwitchPreference enablev14;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            addPreferencesFromResource(R.xml.setting);
            if (Build.VERSION.SDK_INT < 14)
                enable = (CheckBoxPreference) findPreference(key_enable);
            else
                enablev14 = (SwitchPreference) findPreference(key_enable);
        }
        ...
    }
    

    Now my way is to use if-clause to check the Build.VERSION and get the corresponding object to process it. But it is quite inconvenient and hard to manage the code. Do anyone has a smarter way to do it?

  • Phillip Kigenyi
    Phillip Kigenyi over 8 years
    Thanks. Worked perfectly
  • BeniBela
    BeniBela over 6 years
    But TwoStatePreference was added in API level 14, so you cannot use it in the cases you need the CheckBoxPreference