Android : Required API to use Switch?

19,195

Solution 1

There is a nice lecture about this from Google IO 2012 (starting at slide 32)

Here is a detailed example:

Create a separate layout XML file for ICS+ versions by placing it in /res/layout-v14. The resulting file structure will look something like this:

res/layout
- mainlayout.xml
- compound_button.xml
res/layout-v14
- compound_button.xml

Android will then look for resources in the layout-v14 directory when your app is running on v14 or higher.

Place an include in mainlayout.xml that will pull in the pertinent compound_button.xml when the app is run:

<include layout="@layout/compound_button" />

For the pre 4.0 layout we want a checkbox, so create /layout/compound_button.xml as a merge as follows:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >

    <CheckBox
        android:id="@+id/enabled"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enable" />

</merge>

And then for the 4.0+ layout we want a switch, so create /layout-v14/compound_button.xml as a merge as follows:

<?xml version="1.0" encoding="utf-8"?>
<merge 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" >

    <Switch
        android:id="@+id/enabled"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enable"
        tools:ignore="NewApi" />

</merge>

Of course, be sure to set your min and targets appropriately:

<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="14" />

Solution 2

Look for this in your AndroidManifest.xml file:

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16"/>

Change minSdkVersion to 14.

Solution 3

Switch requires API 14, for old version, please use SwithCompat:

<android.support.v7.widget.SwitchCompat
android:id="@+id/switch_compat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:checked="true"
android:textOff="OFF"
android:textOn="ON"
app:showText="false"
android:focusable="false"
android:focusableInTouchMode="false"/>

on setOnCheckedChangeListener:

witchCompat switchCompat = (SwitchCompat)convertView.findViewById(R.id.switch_compat);
    switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                textView.setText("check");
            } else {
                textView.setText("unCheck");
            }
        }
    });

I hope help you.

Solution 4

A better solution is to have two different xml layouts in layouts folder and layouts-v14 folder with same names and the same code. In layouts its toggleButton/checkboxes and in layouts-v14 its switch and dynamically check for the api version and inflate the respective layout.

Solution 5

Your android:minSdkVersion is set to 8. This means your app will run on API-8 (2.2.1) and above but it will crash because Switch is not available

Share:
19,195
Rob
Author by

Rob

Hello everybody.

Updated on June 09, 2022

Comments

  • Rob
    Rob about 2 years

    I can't insert a Switch in my project because of the following error :

    View requires API level 14 (current min is 8):

    But in my project properties, I use Platform 4.1 and API Level 16. So what is wrong?

  • Rob
    Rob almost 12 years
    Thank you for this ! I didn't know it was in the Manifest.
  • azgolfer
    azgolfer almost 12 years
    Note that if you change minSdkVersion to 14, your app will only works with Android 4.0 phones and above. Which currently is only about 10% of all Android phones.
  • Rob
    Rob almost 12 years
    Well I see. So what is the best alternative to Switch ? I think I will have to go with a CheckBox.
  • azgolfer
    azgolfer almost 12 years
    If you want to target older Android versions, you may have to use 'ToggleButton'. Not as nice looking as Switch, but then it works from Android 2.1 and later.
  • John Saunders
    John Saunders over 11 years
    Please provide a summary of what's at that link. Please don't require us to go to another site just to benefit from your answer. And what if the other site goes away or the link breaks?
  • Almer
    Almer over 11 years
    The Switch is only available from API v14 and up. So I guess layout-v14 should be used instead of layout-v11?
  • suomi35
    suomi35 over 11 years
    You are correct, Almer. Switch was not introduced until v14 and so layout-v14 should be used. Nice catch!
  • jbc25
    jbc25 almost 11 years
    The link is broken but the solution is Perfect! Only say that you have to cast CompoundButton in your java class to control both switch and checkbox. Thanks!
  • suomi35
    suomi35 almost 11 years
    Thanks jbc25...Google keeps moving their IO presentations around, but I've updated the link for the second time in a year. Thanks for calling it to my attention :)
  • nolazybits
    nolazybits over 10 years
    Thank you much for the link and explanation!
  • RobinJ
    RobinJ almost 10 years
    This is useless if you have, say, 10 switches, isn't it? You'd have to create a separate file for every switch/checkbox since you can't set their attributes separately in this way.
  • suomi35
    suomi35 over 9 years
    @RobinJ: The XML files above can contain whatever you wish. You can add as many Switches/CompoundButtons as you like and give them whatever attributes you wish.