Backwards compatible Switch

ICS has a Switch component. It does what we need. Is there anything out there backward compatible to (about) 2.2? Couldn’t find anything obvious.

Looks like somebody built this:

  • RemotePlaybackClient, stop() not getting callback
  • Android Studio ProGuard does not appear to run
  • Opencv increasing accuracy of threshold
  • DexFile.class error in eclipse
  • Android textview html font size tag
  • Renaming the package that is generated in

    Related posts:

    How can I avoid garbage collection delays in Java games? (Best Practices)
    Sending intent to BroadcastReceiver from adb
    How do I import a CA certificate into Android 4.4.2 in the emulator?
    How to get the index of particular view OR ViewGroup which is added to the ViewGroup(layout)
    Android - Is it ok to put @IntDef values inside @interface?
    Getting Valid JNIEnv pointer
  • How to add button tint programmatically
  • Configuration `locale` variable is deprecated - Android
  • Scroll up does not work with SwipeRefreshLayout in Listview
  • Why is my android alarm manager firing instantly?
  • Get User Name from Google Plus in android
  • SimpleCursorAdapter deprecated in API version 15?
  • 6 Solutions collect form web for “Backwards compatible Switch”

    Switch is only on 4.0+

    If you want to make an app that uses switch on 4.0+ devices what you need to do is declare two layouts. The first in layout-v14 which will be what’s used on ICS devices. In your layout folder make use of CheckBox.

    In your code make use of the CompoundButton class when getting/setting data from the switch or checkbox. You’ll find that CompoundButton works well for this.

    Android support AppCompat library from version 21.0.0 contains to provide compability back to API v7.

    Include it like this with gradle:

    compile ''

    It can be used in layouts like this:

    < />

    In addition it has showText attribute to make styling easier – which seems to be missing from native andriod Switch.

    This library is what you’re looking for :

    Here is an example of SwitchCompat

    First thing to do make sure you add this lines to your build.gradle and then sync.

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile ''

    Second create a sample Activity , in my case I will call it

    public class SwitchActivity extends ActionBarActivity {
            SwitchCompat mySwitch = null;
            protected void onCreate(Bundle savedInstanceState) {
                // here is your switch
                mySwitch = (SwitchCompat)findViewById(;

    Lastly create your Layout , in my case I will call it activity_switch.xml.

    <RelativeLayout xmlns:android=""
            android:layout_width="wrap_content" />

    If you are using the holoeverywhere library, you can use something like this in your layout file

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.