Posted by & filed under Android, Java.

Update:

I sadly do not have time to maintain the code. Additionally I haven’t worked with Android code for quite some time now. Please check out this awesome project if you want to have the switch and many other cool features.

 

A little time ago I started working on my own Switch button for use in Gingerbread & co. My own version was a little different, because it displayed both sides of the switch instead of only displaying the selected side. I got a lot of feedback for this implementation and some feature requests like disabling the button. So I now completely backported the original Switch for API level 8+, including Froyo and Gingerbread. I also switched to github for this project.

You can either visit the project on GitHub, or download the project here. For documentation and a tutorial on how to use the switch and even in combination with ActionBarSherlock continue reading after the break.

For the backport I simply copied the code from the Api level 15 version of the code. So please make sure to thank the guy who implemented the Switch!

By now the switch has an annoying issue. You can only click it, or use an OnCheckedChangeListener after you added a OnClickListener. If you find a way around this please let me know!

Also it lacks the new Accessibility and right to left text support. Feel free to fork the project or send me a patch.

Installation

Just download the project and extract it. If you’re using the Eclipse Development Environment with the ADT plugin version 0.9.7 or greater you can include the SwitchCompatLibrary as a library project. Create a new Android project in Eclipse using the SwitchCompatLibrary folder as the existing source.
Then go to your project and in your project properties, add the created project under the ‘Libraries’ section of the ‘Android’ category.

Usage

To use the Switch you have to configure your theme. There are several ways!

1. When not using any custom theme

Go to your AndroidManifest.xml and edit the theme to this line:

for the dark theme or to:

for the light theme.

2. When using a custom theme

Go to your styles.xml and make either AppThemeDark or AppThemeLight the parent theme:

3. When using a custom theme and depending on a different parent like ActionBarSherlock

Add the two lines to the style your styles.xml for the light theme

Or add the two lines to the style your styles.xml for the dark theme

 

Trackbacks/Pingbacks

  1.  Switch Button for Android 2.3 Gingerbread - ankri.de