androidandroid-layoutandroid-switch

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


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


Solution

  • 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