Have you tried the Toggle Button? You can configure as a switch button. I use it like this. 1- Open xml in a folder with selection
Your Drawable xml @switch_thumb
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/on" />
<item android:state_checked="false" android:drawable="@drawable/off" />
</selector>
2-set background for the switch in the layout
Your xml layout
<ToggleButton
android:layout_width="60dp"
android:layout_height="30dp"
android:textOn=""
android:textOff=""
android:background="@drawable/switch_thumb"
android:id="@+id/toggle"
/>
3-basic action when checking is turned off and if you want to set marked
mToggle.setChecked(true);
mToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
editor.putBoolean("toggle", mToggle.isChecked());
editor.apply();
}
});
source
share