how to add color to the circle in the switch in android?

1.8k views Asked by At

I want to create a switch which looks like this enter image description here

i have done that in this way

<LinearLayout
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Available"
        android:textColor="#82BCB4"
        android:textSize="20sp"
        />
    <Switch
        android:id="@+id/theSwitchId"
        android:textOn=""
        android:textOff=""
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:thumbTextPadding="5dp"
        android:switchMinWidth="90dp"
        android:layout_marginStart="40dp"
        android:minHeight="60dp"
        android:layout_gravity="center|center_vertical" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="UnAvailable"
        android:layout_marginStart="40dp"
        android:textColor="#224e6d"
        android:textSize="20sp"
        />
</LinearLayout>

i am not able to change the height of the switch and it doesnt look how its in this image. Please help me with this

4

There are 4 answers

1
Subir Chakraborty On BEST ANSWER

all you need to do is just use a custom drawable as track of switch

this is the code for custom drawable:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
    <shape android:shape="rectangle">
        <corners
            android:radius="@dimen/_40sdp" />
        <solid
            android:color="@android:color/darker_gray" />
        <padding
            android:bottom="@dimen/_5dp"
            android:left="@dimen/_5dp"
            android:right="@dimen/_5dp"
            android:top="@dimen/_5dp" />
    </shape>
</item>

Further make these modification to your existing layout add android:thumbTint="@android:color/holo_blue_dark" or any color that meets your requirement

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Available"
    android:textColor="#82BCB4"
    android:textSize="20sp" />

<Switch
    android:id="@+id/theSwitchId"
    android:layout_width="wrap_content"
    android:layout_height="20dp"
    android:layout_gravity="center|center_vertical"
    android:layout_marginStart="40dp"
    android:minHeight="60dp"
    android:switchMinWidth="90dp"
    android:textOff=""
    android:textOn=""
    android:thumbTextPadding="5dp"
    android:thumbTint="@android:color/holo_blue_dark"
    android:track="@drawable/track_background" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="40dp"
    android:text="UnAvailable"
    android:textColor="#224e6d"
    android:textSize="20sp" />

hope this helps

click here to view the resulting screenshot

screenshot

5
Rishabh Mahatha On

In style.xml

   <style name="switchCompatStyle">
        <item name="colorControlActivated">@color/colorPrimary</item>
    </style>

And code for switch is like

 <android.support.v7.widget.SwitchCompat
                android:id="@+id/switch"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="@dimen/_10sdp"
                app:theme="@style/switchCompatStyle" />
0
vasupujy On

You create Thumb in drawable xml: thumb.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:drawable="@drawable/ic_thumbselecter"/>
    <item android:drawable="@drawable/ic_thumbselecter"/>
</selector>

And put into switch (in my case I was using that):

<Switch
    app:kswThumbDrawable="@drawable/thumb"/>
0
Biswas Khayargoli On

Change the thumb (circle on the switch) color by using:

android:thumbTint="@color/thumbColor"

On your colors.xml:

<color name="thumbColor">#FFFFFF</color>