Search code examples
androidandroid-layoutandroid-switch

Creating custom switch


Can anyone help me out in creating a switch like the one shown below?

enter image description here

The button in the switch can be dragged to the other side as well. It will change position on click just as the button crosses half from its current position. The text in the button also changes. Here is what I am referring to:

enter image description here

I've researched on switches on Android but none of them gave me this result. I've also looked at the toggle button but that did not do the trick either.


Solution

  • You got the answer in this link How to custom switch button?!

    Just use the Selectorto define the On/Off states of the Switch as follows:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="false" android:drawable="@drawable/switch_off"  />
        <item android:state_checked="true" android:drawable="@drawable/switch_on"  />
    </selector>