try this
Create the colors.xml file in the res/values folder:
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="red">#ff0000</color> <color name="green">#00ff00</color> </resources>
In the drawable folder, the file my_btn_toggle.xml is created:
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="false" android:drawable="@color/red" /> <item android:state_checked="true" android:drawable="@color/green" /> </selector>
and in xml add ToggleButton in the form:
android:background="@drawable/my_btn_toggle
a source
There is an option to create your own ToggleButton
activity_main.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" tools:context=".MainActivity" android:background="#d1d4d4" > <!-- ToggleButton по умолчанию --> <ToggleButton android:id="@+id/toggle" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!-- ToggleButton наш --> <ToggleButton android:id="@+id/toggle2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/toggle_selector" android:layout_below="@id/toggle" /> </RelativeLayout>
res/drawable/toggle_selector.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/toggle_state_on" android:state_checked="true" /> <item android:drawable="@drawable/toggle_state_off" android:state_checked="false" /> </selector>
res/drawable/toggle_state_on.xml
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <!-- 5dp border вокруг фигуры --> <stroke android:color="#4c975d" android:width="5dp" /> </shape> </item> <!-- Переписать левую, верхную, правую стороны с фоном нашего цвета --> <item android:bottom="5dp" > <shape android:shape="rectangle"> <solid android:color="#6dd988"/> </shape> </item> </layer-list>
res/drawable/toggle_state_off.xml
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <!-- 5dp border вокруг фигуры --> <stroke android:color="#b1113e" android:width="5dp" /> </shape> </item> <!-- Переписать левую, верхную, правую стороны с фоном нашего цвета --> <item android:bottom="5dp" > <shape android:shape="rectangle"> <solid android:color="#f51354"/> </shape> </item> </layer-list>
the result will be something like this

a source