Android toggle button custom look

Mihai Bratulescu picture Mihai Bratulescu · Aug 20, 2013 · Viewed 96.5k times · Source

I've been trying to customize the toggle button look but with no success. Here is how I want it to look like:

enter image description here

Can someone give me a tutorial?

Answer

Melbourne Lopes picture Melbourne Lopes · Nov 30, 2013

create toggle_selector.xml in res/drawable

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

apply the selector to your toggle button

<ToggleButton
            android:id="@+id/chkState"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/toggle_selector"
            android:textOff=""
            android:textOn=""/>

Note: for removing the text i used following in above code

textOff=""
textOn=""