Since the appcompat v7 is missing a SwitchCompatPreference
it seems like it's necessary to create it by myself.
How can this be achieved? I googled a bit and found a tutorial for a DialogPreference
. I tried to adopt it for a SwitchCompatPreference
but in my xml layout it always says that this class is not allowed in the preference xml.
What do I need to do?
You do not need to create a new component.
First of all, you should use
CheckBoxPreference
instead of SwitchPreference, in order to support lower APIs.Using the existing
android.support.v7.widget.SwitchCompat
widget, create a new layout file, for examplel_switch.xml
. Use the following code:Then, to your
SwitchPreferenceCheckBoxPreference inPreferenceFragment
,or, to your CheckBoxPreference directly,
This will force the CheckBoxPreference to use the
SwitchCompat
style.