How to Custom Switch Button

How to custom switch button?

However, I might not be taking the best approach, but this is how I have created some Switch like UIs in few of my apps.
Here is the code -

<RadioGroup
android:checkedButton="@+id/offer"
android:id="@+id/toggle"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginBottom="@dimen/margin_medium"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="@dimen/margin_medium"
android:background="@drawable/pink_out_line"
android:orientation="horizontal">

<RadioButton
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:id="@+id/search"
android:background="@drawable/toggle_widget_background"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="Search"
android:textColor="@color/white" />

<RadioButton
android:layout_marginRight="1dp"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:id="@+id/offer"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/toggle_widget_background"
android:button="@null"
android:gravity="center"
android:text="Offers"
android:textColor="@color/white" />
</RadioGroup>

pink_out_line.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="2dp" />
<solid android:color="#80000000" />
<stroke
android:width="1dp"
android:color="@color/pink" />
</shape>

toggle_widget_background.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/pink" android:state_checked="true" />
<item android:drawable="@color/dark_pink" android:state_pressed="true" />
<item android:drawable="@color/transparent" />
</selector>

And here is the output -Sample Image

How to customize switch in android

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>

Android Custom Switch

After researching I found a way that gives me exactly what I needed, this is what I got:

custom switch

in case of anyone looking for a way to do it, this is how:

based on this post answer , which worked great for me.

this is what I did, I created two drawables one for On and another for Off :

switch_on.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:state_checked="true" android:drawable="@color/colorGray"/>
<item android:drawable="@color/colorPrimary" android:state_checked="true" />
<item android:drawable="@color/colorPrimaryDark" android:state_pressed="true" />
<item android:drawable="@color/transparent" />

</selector>

switch_off.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:state_checked="true" android:drawable="@color/colorGray"/>
<item android:drawable="@color/gray_light" android:state_checked="true" />
<item android:drawable="@color/black_overlay" android:state_pressed="true" />
<item android:drawable="@color/transparent" />
</selector>

Next , created a drawable for the outline of the switch.
outline.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="2dp" />
<solid android:color="#80ffffff" />
<stroke
android:width="1dp"
android:color="@color/gray_light" />
</shape>

one thing that I added is a drawable for the text color, because the color of the text changes depending on whether it's checked or not, this is it :
switch_text.xml

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

and finally, created RadioGroup in my layout this way:

<RadioGroup
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="@drawable/outline"
android:checkedButton="@+id/off"
android:orientation="horizontal">

<RadioButton
android:id="@+id/off"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginBottom="1dp"
android:layout_marginStart="1dp"
android:layout_marginTop="1dp"
android:layout_weight="1"
android:background="@drawable/switch_off"
android:button="@null"
android:gravity="center"
android:padding="@dimen/fab_margin"
android:text="@string/off"
android:textColor="@drawable/switch_text" />

<RadioButton
android:id="@+id/on"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginBottom="1dp"
android:layout_marginEnd="1dp"
android:layout_marginTop="1dp"
android:layout_weight="1"
android:background="@drawable/switch_on"
android:button="@null"
android:gravity="center"
android:padding="@dimen/fab_margin"
android:text="@string/on"
android:textColor="@drawable/switch_text" />
</RadioGroup>

Notice the usage of each drawable in the right place:

android:background="@drawable/outline" for the RadioGroup
android:background="@drawable/switch_off" for the first RadioButton
android:background="@drawable/switch_on" for the second RadioButton
android:textColor="@drawable/switch_text" for both Radio Buttons

And that's all.

Android Switch Button Custom design

I used below gihub library for this type of button

https://github.com/kyleduo/SwitchButton

Advantage:

  • customize as per color you want
  • with good animation of switching button


Related Topics



Leave a reply



Submit