Java
DeviceSwitch.setLayoutResource(R.layout.settings);
DeviceSwitch.setKey(CategoryKey);
DeviceSwitch.setDefaultValue(true);
DeviceSwitch.setEnabled(true);
DeviceSwitch.setSelectable(true);
DevicesShowScreen.addPreference(DeviceSwitch);
if this code run --> DeviceSwitch.setLayoutResource(R.layout.settings); Switch not checked in Android 6.0
This string --> DeviceSwitch.setCheked(true); Not work in Android 6.0
in Android 7.0 switch preference from layout checked normal
Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/listPreferredItemHeightSmall"
android:gravity="center_vertical"
android:clipToPadding="false">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_marginStart="15dp">
<TextView android:id="@android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?attr/textAppearanceListItem"
android:ellipsize="marquee" />
<TextView android:id="@android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@android:id/title"
android:layout_alignStart="@android:id/title"
android:maxLines="10"
android:ellipsize="end" />
</RelativeLayout>
<Switch
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:focusable="false"
android:clickable="false"
android:gravity="center"
android:layout_marginEnd="15dp"
android:id="@android:id/switch_widget"
android:checked="false" />
</LinearLayout>
It's because sdk 23 uses com.android.internal.R.id.switchWidget identifier instead of com.android.internal.R.id.switch_widget
Workaround is quite simple. Just inherit SwitchPreference class and duplicate some code:
And of course don't forget to replace
android:id="@android:id/switch_widget"
toandroid:id="@+id/switch_widget"
in your widget switcher layout.