15

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?

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Rob
  • 15,732
  • 22
  • 69
  • 107
  • Check out the solution here: http://stackoverflow.com/questions/9920709/use-android-4-0-styled-toggle-button/15640365#15640365 – dberm22 Mar 26 '13 at 16:53

6 Answers6

74

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" />
suomi35
  • 1,069
  • 12
  • 11
  • 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? – John Saunders Oct 15 '12 at 18:57
  • The Switch is only available from API v14 and up. So I guess layout-v14 should be used instead of layout-v11? – Almer Dec 13 '12 at 15:33
  • You are correct, Almer. Switch was not introduced until v14 and so layout-v14 should be used. Nice catch! – suomi35 Dec 30 '12 at 04:51
  • 3
    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! – jbc25 Aug 07 '13 at 16:26
  • 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 :) – suomi35 Sep 09 '13 at 18:47
  • Thank you much for the link and explanation! – nolazybits Oct 09 '13 at 03:30
  • 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. – RobinJ Aug 25 '14 at 18:07
  • @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. – suomi35 Jan 09 '15 at 23:45
5

Look for this in your AndroidManifest.xml file:

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

Change minSdkVersion to 14.

azgolfer
  • 15,087
  • 4
  • 49
  • 46
  • Thank you for this ! I didn't know it was in the Manifest. – Rob Jul 11 '12 at 14:38
  • 1
    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. – azgolfer Jul 11 '12 at 14:44
  • Well I see. So what is the best alternative to Switch ? I think I will have to go with a CheckBox. – Rob Jul 11 '12 at 14:46
  • 2
    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. – azgolfer Jul 11 '12 at 14:54
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.

tvtruong
  • 99
  • 1
  • 4
1

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

chrulri
  • 1,822
  • 14
  • 16
1

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.

0

Look there are two ways to solve your problem first either you change your minSdkVersion <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16"/> from 8 to 14 or define another layout containing Switch in layout-v14 you can create manually this folder in res directory

Irshad Khan
  • 794
  • 6
  • 21