Search code examples
androidtogglebutton

Toggle button in Iphone Style


In android the Toggle buttons are looks like below -

Android

Can we modify this as Iphone style like below -

Iphone

And, Can we include the iphone functionality of toggle button like drag to toggle feature also.

How to complete this action? Thanks in Advance.


Solution

  • You just have to provide 2 drawables.

    <ToggleButton 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/toggle_me"/>
    

    and the drawable will be something like:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true"
            android:drawable="@drawable/toggle_me_on" /> <!-- checked -->
        <item android:drawable="@drawable/toggle_me_off" /> <!-- default/unchecked -->
    </selector>
    

    Unfortunately, this solution doesn't provide great transition effects, but will give you exactly what you asked.