Create own SwitchCompat Preference
Asked Answered
G

1

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?

Goldstein answered 3/11, 2014 at 22:36 Comment(1)
I have edited my answer. Please use the edited answer.Driskill
D
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.

Driskill answered 13/11, 2014 at 10:58 Comment(9)
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?Pleasant
I have the same issue. I'll find a fix, and update this post.Driskill
This works but you lose the switch's toggle animation. I really hope they just create a SwitchCompatPreference in the near future.Pleasant
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.Driskill
That's what I'm currently doing.Pleasant
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.Whereunto
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.Whereunto
To make the switch animate, see my reply here: #26537009Potty
@Potty yes, animations would depend on additional code. I have provided just the basis.Driskill

© 2022 - 2024 — McMap. All rights reserved.