Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : Required API to use Switch?

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?

like image 471
Rob Avatar asked Jul 11 '12 14:07

Rob


4 Answers

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" />
like image 117
suomi35 Avatar answered Oct 02 '22 20:10

suomi35


Look for this in your AndroidManifest.xml file:

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

Change minSdkVersion to 14.

like image 20
azgolfer Avatar answered Oct 02 '22 19:10

azgolfer


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.

like image 3
tvtruong Avatar answered Oct 02 '22 20:10

tvtruong


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

like image 1
chrulri Avatar answered Oct 02 '22 21:10

chrulri