android – Backwards compatible Switch-ThrowExceptions

Exception or error:

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

How to solve:

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 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.

###

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)

###

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

###

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>

###

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"
 />

Leave a Reply

Your email address will not be published. Required fields are marked *