I need to implement this type of switch in my app
            Asked
            
        
        
            Active
            
        
            Viewed 1.5k times
        
    11
            
            
        
        Solanki Zeel
        
- 548
 - 1
 - 7
 - 18
 
- 
                    **Welcome to Stack Overflow**, well your question shows lack of research here, and **`FYI`** StackOverflow is neither a forum, tutorial site nor web search replacement. We can **[help with certain problems](https://stackoverflow.com/help/on-topic)**, but it's your job to **[put some efforts](https://meta.stackoverflow.com/q/261592/7666442)** in the first place, including elementary **[(re)search](https://www.google.co.in/)**. Read **[how to ask perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/)** – AskNilesh Feb 26 '19 at 06:45
 - 
                    Possible duplicate of [How to custom switch button?](https://stackoverflow.com/questions/23358822/how-to-custom-switch-button) – V-rund Puro-hit Feb 26 '19 at 06:48
 - 
                    Take a look at this library it can do exactly what you want https://github.com/Angads25/android-toggle – Mr. Patel Feb 26 '19 at 06:53
 
1 Answers
30
            You can use below given code. You might need to adjust height and width in thumb_selector file
<android.support.v7.widget.SwitchCompat
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:thumb="@drawable/thumb_selector"
    app:track="@drawable/track_selector" />
track_selector.xml
    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true">
        <shape android:dither="true" android:shape="rectangle" android:useLevel="false" android:visible="true">
            <solid android:color="@color/light_pink" />
            <corners android:radius="50dp" />
            <size android:width="2dp" android:height="24dp" />
        </shape>
    </item>
    <item android:state_checked="false">
        <shape android:dither="true" android:shape="rectangle" android:useLevel="false" android:visible="true">
            <corners android:radius="50dp" />
            <size android:width="2dp" android:height="24dp" />
            <stroke
                android:width="2dp"
                android:color="@color/white" />
        </shape>
    </item>
</selector>
thumb_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false">
        <shape android:dither="true" android:shape="rectangle" android:useLevel="false" android:visible="true">
            <solid android:color="#ffffff" />
            <corners android:radius="100dp" />
            <size android:width="18dp" android:height="18dp" />
            <stroke android:width="4dp" android:color="#0000ffff" />
        </shape>
    </item>
</selector>
        Rahul Khurana
        
- 8,577
 - 7
 - 33
 - 60
 
- 
                    1Using Rectangle here is not correct. Please use android:shape="oval" – android6p Mar 25 '21 at 07:07
 
