Menachem Yarhi
Menachem Yarhi

Reputation: 63

How to change the background color of the switch item when it's on off mode?

How to change the background color of the switch item when it's on off mode and how to add text in right?

I have tried a lot of things and I can't find a solution.

Maybe there is a custom switch for it?

Thanks.

Upvotes: 3

Views: 188

Answers (2)

yuzurihaaa93
yuzurihaaa93

Reputation: 465

to set the color of of the switch background

create a drawable files switch_track.xml for switch background

<?xml version="1.0" encoding="utf-8"?> 
<selector
        xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:color="@color/background_color_checked"
            android:state_checked="true" />
        <item android:color="@color/background_color_not_checked"/> 
</selector>

create a drawable files switch_thumb.xml for switch thumb

<?xml version="1.0" encoding="utf-8"?> 
<selector
     xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:color="@color/thumb_color_checked"
           android:state_checked="true" />
     <item android:color="@color/thumb_color_not_checked"/> 
</selector>

create a style for the switch

<style name="myCustomStyle" >
        <item name="thumbTint">@drawable/switch_thumb</item>
        <item name="trackTint">@drawable/switch_track</item>
</style>

and you can apply it on the switch

<android.support.v7.widget.SwitchCompat
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/myCustomStyle" />

Upvotes: 1

Wasim K. Memon
Wasim K. Memon

Reputation: 6067

First of all add some code or whatever you have tried so we can help you with that only.

2nd Here are some of the tutorials to customize switches in android.

Link 1, Link 2

Upvotes: 1

Related Questions