user2436032
user2436032

Reputation: 365

Specify target API level in Layout xml 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
    }
    

Upvotes: 9

Views: 11611

Answers (1)

Kundan
Kundan

Reputation: 590

you can use the tools target api

<item name="android:fontFamily" tools:targetApi="jelly_bean">sans-serif-light</item>

this will also work

<item name="android:fontFamily" tools:targetApi="17">sans-serif-light</item>

Upvotes: 13

Related Questions