Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
A backport of the SwitchPreference component that was introduced on Android 4 (ICS / level 14). This port works on Android 2.1+ (Eclair MR1 / level 7).
branch: master

Android Switch Widget Backport

A backport of the Switch widget ( that was introduced on Android 4.

This port works on Android 2.1+.

The current version of this library is 1.4.0.

IMPORTANT The 1.4.0 release has an incompatible change that requires you to update your themes.xml and layout files. Specifically, switchStyle has been renamed to asb_switchStyle, and switchPreferenceStyle becomes asb_switchPreferenceStyle. Similarly, all the switch attributes have been prefixed so switchTextOn is now asb_switchTextOn, and so on.

I am sorry about this but this has been necessary to avoid conflicts with the latest support library.

How to use

Adding the library to your project

Option 1: Gradle

The aar artifact is available at the jcenter repository. Declare the repository and the dependency in your build.gradle file:

repositories {


dependencies {
    compile 'org.jraf:android-switch-backport:1.4.0'

Option 2: Android library project (ant / Eclipse)

This is an Android library project, you have to add it as a dependency to your project (please see this page to know how to do that.)

Option 3: Maven

The apklib artifact is available at the jcenter repository. Declare the repository and the dependency in your pom.xml file:




Note: the artifacts used to be hosted on the repository, but due to server problems, they are now hosted on jcenter. Please update your repository declarations!

Using the Switch

Once you have done that, have a theme for your application (or Activity), that declares the asb_switchStyle item to be one of the two possible themes: either Widget.Holo.CompoundButton.Switch (dark) or Widget.Holo.Light.CompoundButton.Switch (light).

The simplest way to do that is to create a themes.xml file in your project's res/values folder with this contents:

<?xml version="1.0" encoding="utf-8"?>

    <style name="Theme" parent="@android:style/Theme">
        <item name="asb_switchStyle">@style/Widget.Holo.CompoundButton.Switch</item>


And use it in your Application or Activity by updating your AndroidManifest.xml file:




Then in your layout xml files you use the widget like this:

    android:layout_height="wrap_content" />

Using the SwitchPreference

Add asb_switchPreferenceStyle to your 'themes.xml'

<?xml version="1.0" encoding="utf-8"?>

    <style name="Theme" parent="@android:style/Theme">
        <item name="asb_switchStyle">@style/Widget.Holo.CompoundButton.Switch</item>
        <item name="asb_switchPreferenceStyle">@style/Preference.SwitchPreference</item>


Then in your preference xml file:

<PreferenceScreen xmlns:android=""
    xmlns:switchpref="" >

        android:title="SwitchPreference Test"
        switchpref:asb_summaryOn="@string/summary_on" />


Sample app

A sample app is available in the sample folder, and also on the Play Store: Get it on Google Play


The code was copied directly from the Android 4.0.3 (API 15) source code, then slightly tweaked by myself ( to make it run on 2.1+. The few modifications I made are documented in the code (look for 'XXX' comments). The SwitchPreference part was added later by Intrications ( /, also by taking code from Android and tweaking it a bit. Other people also have contributed tweaks and fixes, please see this page for a detailed list:


Pull requests are welcome, as long as they are consistent to the original Switch / SwitchPreference of the Android sdk.

Please do not contribute improvements that are not present in the original sdk classes! I believe it would be confusing for this backport to have a feature set different than the original sdk classes. It would also lead to difficult situations if/when stopping using this backport and using the sdk classes instead (which should happen when dropping support for old platforms). Thank you very much.


Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Something went wrong with that request. Please try again.