##Switch for Froyo, Gingerbread & Honeycomb## A little while ago I started my own custom Switch button for Gingerbread. I wanted to have a switch button that always displays the texts on the left and right side. Since the standard Switch only shows the text from the currently selected side, I had to code my own version, which you can check out here: http://ankri.de/switch-button-for-android-2-3-gingerbread/
Unfortunately it isn't perfect. You cannot disable it for example, because I didn't need it in my use case. After I got some feedback, I decided to do a complete backport of the original Switch button, which was fairly easy! I just copied the code and edited some lines.
I present to you the backport of the Switch button. Which has two minor limitations
- the accessibility functionality doesn't work with the Switch
- the Switch does not have RTL support
Yeah. Unfortunately there is a minor bug aswell. For some reason the OnCheckedChangeListener
is only called when the Switch has an OnClickListener
. As a workaround I added an OnClickListener
in the constructor of the Switch, so you don't have to worry about it.
There are two projects. The library
and the demo
project. You can use the library
project just like any other library project.
Just download the project. 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.
To use the Switch you have to configure your theme. There are several ways!
Go to yourAndroidManifest.xml
and edit the theme to this line:
android:theme="@style/AppThemeDark"for the dark theme or to:
android:theme="@style/AppThemeLight"for the light theme. Go to your
styles.xml
and make either AppThemeDark
or AppThemeLight
the parent theme:
<style name="AppTheme" parent="AppThemeLight" />Add the two lines to the style your
styles.xml
for the light theme
<style name="AppTheme" parent="Theme.Sherlock.Light"> <item name="switchStyle">@style/switch_light</item> <item name="textAppearance">@style/TextAppearance</item> </style>Or add the two lines to the style your
styles.xml
for the dark theme
<style name="AppTheme" parent="Theme.Sherlock"> <item name="switchStyle">@style/switch_dark</item> <item name="textAppearance">@style/TextAppearance</item> </style>