Chiman
Chiman

Reputation: 163

SwitchPreference and CheckBoxPreference in code

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?

Upvotes: 9

Views: 25270

Answers (3)

AleXqwq
AleXqwq

Reputation: 271

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);
    }
    ...
}

Upvotes: 6

Mark Larter
Mark Larter

Reputation: 2363

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.

Upvotes: 1

Renan Ferrari
Renan Ferrari

Reputation: 2519

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.

Upvotes: 14

Related Questions