Pregunta

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?

¿Fue útil?

Solución

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

Otros consejos

Look for this in your AndroidManifest.xml file:

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

Change minSdkVersion to 14.

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.

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

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.

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top