19

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:

https://github.com/Prototik/KFramework-SW.git

2

6 Answers 6

44

Android support AppCompat library from version 21.0.0 contains android.support.v7.widget.SwitchCompat to provide compability back to API v7. https://developer.android.com/reference/android/support/v7/widget/SwitchCompat.html

Include it like this with gradle:

compile 'com.android.support:appcompat-v7:21.0.0'

It can be used in layouts like this:

<android.support.v7.widget.SwitchCompat />

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

1
  • It work for me,SwitchCompat show switch at 4.x sytle(not look ugly) like 5.x 6.x (look well), It nice change Switch to SwitchCompat!
    – shuabing
    Mar 23, 2017 at 8:53
10

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.

3
  • I don't understand your answer. I understand the folder naming conventions. I don't understand what you're getting at with CompoundButton. In summary, the answer is "no", correct? May 11, 2012 at 23:31
  • 1
    I was trying to say that you can have code that uses switch on ICS but uses checkbox on Pre ICS. But yes, in short the answer is "no".
    – Lee
    May 12, 2012 at 9:09
  • Just to confirm, as of today, no open source compatibility resource. If I get down time, may create one. Really like the way it works. Sep 6, 2012 at 3:26
4

you should use checkbox when such a thing is impossible , as described here:

https://docs.google.com/a/android.co.il/presentation/d/1mKmwM-HNXukKT_FgAMmyCuwMdL4nQI4aZ6SXIr5wixc/pub?start=false&loop=false&delayms=3000#slide=id.g119cf79b_0_8

(slide 32)

4

This library is what you're looking for : https://github.com/BoD/android-switch-backport

3
  • I tried the first one and it works well. The quality of the code could be better (almost no javadoc, difficult to understand sometimes, difficult to modify). But the lib is overall well done and offers switch that can be fully themed. We got a fine result via Android Holo Colors.
    – Snicolas
    Mar 25, 2013 at 17:08
  • I tried both and they work quite well, except a serious issue on some devices: first time an activity properly themed to include switches, the app crashes because one of the text attribute is not found resulting in a null pointer for the text content! as if the theme was not set, does anyone faced this and have a solution?
    – 3c71
    May 10, 2013 at 14:22
  • 1
    This should be reported to the issue of the github repo of the project. Not here.
    – Snicolas
    May 10, 2013 at 15:40
4

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 'com.android.support:appcompat-v7:22.0.0'
}

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

public class SwitchActivity extends ActionBarActivity {

        SwitchCompat mySwitch = null;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_switch);

            // here is your switch
            mySwitch = (SwitchCompat)findViewById(R.id.myswitch);

        }
        .....
}

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.neoecosystem.samplex.SwitchActivity">

    <android.support.v7.widget.SwitchCompat
        android:id="@+id/myswitch"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" />

</RelativeLayout>
0

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

<org.holoeverywhere.widget.Switch
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
 />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.