Slide Toggle for Android

Slide Toggle for Android

iOS does not seem to have a "slide toggle", at least under that name, based on a Google search. And, you did not provide an image (or a link to an image) of what you want.

Android 4.0 added a Switch that you might be able to backport to earlier versions. You will see samples of it in the API Demos app on your emulator:

Sample Image

Android: How to create slide (on/off) button

Well it seems that Switch component is the best solution if your target SDK is higher than 4.0 (Ice Cream Sandwich). So for the others who will face the same problem look at it. :)

Create an toggle / sliding button

Follow the three steps and after it all your app's togglebutton got the new style!

toggle.xml: (drawable-folder)

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

styles.xml: (values-folder)

<style name="toggle" parent="@android:style/Widget.Button.Toggle">
<item name="android:background">@drawable/toggle</item>
<item name="android:textOn"> </item>
<item name="android:textOff"> </item>
</style>

and last step you have to teach the current AppTheme that you have a custom toggle-button:

<style name="AppTheme" parent="android:Theme.Holo.Light">
<item name="android:buttonStyleToggle">@style/toggle</item>
</style>

Toggle button slider with multi state. Left and Right

Solved it using customized seekbar.

Use Android 4.0 styled toggle-button

New Edit: I now did a full backport of the Switch back to API Level 8 and put in on github:
https://github.com/ankri/SwitchCompatLibrary


The old post with my custom implementation of the Switch:

I'm a bit late to the party but I had the same problem. I took the source code from the other post in this thred and made my own version of the switch.

You can find the source code and documentation on my website

This is what it looks like:

SwitchButtonDemo screenshot

edit: Updated link and picture



Related Topics



Leave a reply



Submit