Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify target API level in Layout xml android?

Tags:

android

I needed to add a digital clock in the layout. My min-supported API level is 14. I found out that there is a digital clock widget for API level below 17 (After that it got deprecated), and there is a text clock for API 17 and above.

So one thing I can do is to add both layout in xml and dynamically set the visibility based on version number in the code. But the xml will continuously show me that I have used Text Clock while my min-supported level is 14. So:

  1. Is there a way in the xml to specify to ignore the min-sdk error. Something like

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    

That we use in the normal java code.

  1. Is there a way in XML to specify to use a particular widget based on version number. Some kind of if else statement in the xml that says:

    if((Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        //Use text clock
    } else {
        // Use digital clock
    }
    
like image 751
user2436032 Avatar asked May 25 '15 08:05

user2436032


People also ask

What is target API level?

When you upload an APK, it must meet Google Play's target API level requirements. New apps must target Android 12 (API level 31) or higher; except for Wear OS apps, which must target Android 11 (API level 30) or higher.

Which of the following tag in XML specifies the minimum and maximum level of the API for your application?

android:minSdkVersion — Specifies the minimum API Level on which the application is able to run. The default value is "1". android:targetSdkVersion — Specifies the API Level on which the application is designed to run.

What is API 30 in Android?

It is the minimum SDK version that the app support. It is defined in build. gradle file. For example, if you set the minSdk to 30, an SDK version corresponds to API Level 30 and Android 11. It makes your app run only on devices with Android 8 or higher versions.


1 Answers

If you want to show different layout depending api version, you can also name your resource folders to indicate version number like

/res/layout/layout.xml       (Default)

/res/layout-v14/layout.xml

/res/layout-v17/layout.xml

like image 87
jlopez Avatar answered Sep 23 '22 09:09

jlopez