Android 6.0 not checked Switch preference from layout resource. Android 7.0 work fine

638 views Asked by At

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>
1

There are 1 answers

0
Alexander Suslov On

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:

public class MySwitchPreference extends SwitchPreference {
    private final Listener mListener = new Listener();

    private class Listener implements CompoundButton.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (!callChangeListener(isChecked)) {
                // Listener didn't like it, change it back.
                // CompoundButton will make sure we don't recurse.
                buttonView.setChecked(!isChecked);
                return;
            }

            MySwitchPreference.this.setChecked(isChecked);
        }
    }

    public MySwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
       super(context, attrs, defStyleAttr, defStyleRes);
    }

    public MySwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MySwitchPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MySwitchPreference(Context context) {
        super(context);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);

        View checkableView = view.findViewById(R.id.switch_widget);
        if (checkableView != null && checkableView instanceof Checkable) {
            if (checkableView instanceof Switch) {
                final Switch switchView = (Switch) checkableView;
                switchView.setOnCheckedChangeListener(null);
            }

            ((Checkable) checkableView).setChecked(isChecked());

            if (checkableView instanceof Switch) {
                final Switch switchView = (Switch) checkableView;
                switchView.setTextOn(getSwitchTextOn());
                switchView.setTextOff(getSwitchTextOff());
                switchView.setOnCheckedChangeListener(mListener);
            }
        }
    }
}

And of course don't forget to replace android:id="@android:id/switch_widget" to android:id="@+id/switch_widget" in your widget switcher layout.