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?
This settings screen is used to manage the preferences of the users. For creating this settings screen android provides a feature to make a settings preferences screen. In this article, we will take a look at implementing the preferences setting screen in Android.
androidx.preference.SwitchPreference. A Preference that provides a two-state toggleable option. This preference will save a boolean value to android. content. SharedPreferences .
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.
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);
    }
    ...
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With