5

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?

vovahost
  • 34,185
  • 17
  • 113
  • 116
theknut
  • 2,533
  • 5
  • 26
  • 41

1 Answers1

24

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 example l_switch.xml. Use the following code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.SwitchCompat xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/checkbox" <!-- IMPORTANT -->
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@null"
    android:clickable="false" <!-- IMPORTANT -->
    android:focusable="false" <!-- IMPORTANT -->
    android:gravity="center" />

Then, to your SwitchPreference CheckBoxPreference in PreferenceFragment,

yourSwitch = findPreference("key_for_this_component");
yourSwitch.setWidgetLayoutResource(R.layout.l_switch);

or, to your CheckBoxPreference directly,

android:widgetLayout="@layout/l_switch"

This will force the CheckBoxPreference to use the SwitchCompat style.

jyoonPro
  • 1,661
  • 1
  • 16
  • 41
  • I'm using this code, and while it does display the new SwitchCompat widget, the preference never actually persists. In your project, are you certain this is actually persisting the preference changes? – JDJ Dec 06 '14 at 19:52
  • 2
    I have the same issue. I'll find a fix, and update this post. – jyoonPro Dec 06 '14 at 20:15
  • This works but you lose the switch's toggle animation. I really hope they just create a SwitchCompatPreference in the near future. – JDJ Dec 14 '14 at 16:44
  • 1
    You can create a `xml-v21` file and add SwitchPreference there, while the original would have CheckBoxPreference. That way, you don't lose animation on lollipop. – jyoonPro Dec 14 '14 at 16:53
  • That's what I'm currently doing. – JDJ Dec 14 '14 at 16:54
  • I'm seeing an odd behavior with this technique. If I have a CheckBoxPreference using the SwitchCompat style, and I change its state, its switch will re-animate to its current value (one time) the next time another setting is changed. For example, if the switch is in the off position, and I switch it on, it animates to the on position as expected. But then if I change any other preference's value, the switch will re-animate from the off position to the on position. After that, changes to other preferences have no effect on the switch, until the switch's value is changed again. – Andy Dennie Mar 23 '15 at 14:54
  • Addendum to above comment: I'm only noticing this on Lollipop, and interestingly, the phenomenon only applies to the last CheckBoxPreference using the SwitchCompat style on the screen. – Andy Dennie Mar 23 '15 at 14:54
  • To make the switch animate, see my reply here: https://stackoverflow.com/questions/26536509/android-appcompat-v21-provides-switchcompat-does-not-provide-switchcompatperefre/31229920#31229920 – TiborP Jul 05 '15 at 11:40
  • @TiborP yes, animations would depend on additional code. I have provided just the basis. – jyoonPro Jul 05 '15 at 12:12