visit
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="//schemas.android.com/apk/res/android"
xmlns:app="//schemas.android.com/apk/res-auto"
xmlns:tools="//schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<androidx.appcompat.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/customSwitch"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:track="@drawable/track"
android:thumb="@drawable/thumb"
android:text="Custom switch "
/>
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="//schemas.android.com/apk/res/android">
<item android:state_checked="false">
<shape android:shape="rectangle">
<solid android:color="@android:color/white"/>
<corners android:radius="100sp"/>
<stroke android:color="#8e8e8e"
android:width="1dp"/>
</shape>
</item>
<item android:state_checked="true">
<shape android:shape="rectangle">
<solid android:color="@color/colorAccent"/>
<corners android:radius="100sp"/>
</shape>
</item>
</selector>
In the above code, we made two items in the selector where the first item represents the state when the switch is off or not checked and the second item represents the state when the switch is on or checked.
In the first item, we made a shape of a rectangle and fill white color with having corner radius with a grey color stroke or outline and in the second item the only difference is filled color will be our accent color and stroke will be removed but the corner radius will be the same.Now let's make our thumb part where we will add images on the thumb.<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="//schemas.android.com/apk/res/android">
<item android:state_checked="false"
android:drawable="@drawable/switch_thumb_false"/>
<item android:state_checked="true"
android:drawable="@drawable/switch_thumb_true"/>
</selector>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="//schemas.android.com/apk/res/android">
<item >
<shape android:shape="oval">
<solid android:color="@color/colorAccent"/>
<size android:height="48dp"
android:width="48dp"/>
</shape>
</item>
<item android:drawable="@drawable/ic_notifications_off"
android:bottom="12dp"
android:top="12dp"
android:left="12dp"
android:right="12dp"
/>
</layer-list>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="//schemas.android.com/apk/res/android">
<item >
<shape android:shape="oval">
<solid android:color="@color/colorAccent"/>
<size android:height="48dp"
android:width="48dp"/>
<stroke android:width="1sp"
android:color="#8e8e8e"
/>
</shape>
</item>
<item android:drawable="@drawable/ic_notification_on"
android:bottom="12dp"
android:top="12dp"
android:left="12dp"
android:right="12dp"
/>
</layer-list>
Like thumb false, this code is similar to it but the differences are we added the stroke to the round shape and of course change the image drawable.
Now we have made the all necessary files now its time to run our app and your custom switch is ready. Tada!!!!
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="//schemas.android.com/apk/res/android">
<item android:state_checked="false"
android:drawable="@drawable/track_off"
>
</item>
<item android:state_checked="true" android:drawable="@drawable/track_on">
</item>
</selector>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="//schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle"
>
</shape>
</item>
<item
android:top="12sp"
android:bottom="12sp"
>
<shape android:shape="rectangle"
>
<solid android:color="@android:color/white"/>
<corners android:radius="100sp"/>
<stroke android:color="#8e8e8e"
android:width="1dp"/>
</shape>
</item>
</layer-list>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="//schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
</shape>
</item>
<item
android:top="12sp"
android:bottom="12sp"
>
<shape android:shape="rectangle">
<solid android:color="@color/colorAccent"/>
<corners android:radius="100sp"/>
</shape>
</item>
</layer-list>
Previously published at