2

Sometimes, when I want to add SDK 21+ feature to my layout, I need to create whole layout in another file. It's heavy to me because I want to do or check everything in one layout. More of layouts are looking complex and hard to manage. Instead of having two layouts for different SDK versions, can I do something like this:

<ImageView
    android:id="@+id/x"
    android:layout_width="16dp"
    android:layout_height="wrap_content"
    <compatibility sdk_higher_than="21">
        android:elevation="xdp" //my problem not about the elevation. Its just an example that pops in my mind about the compatibility.
    </compatibility>
    app:srcCompat="@drawable/ic_x" />

I can make this stuff programmatically but when I should see the view instantly on designer, making it programmatically is not a good way for me. If there is a good practice or idea for this problem can anybody illuminate me?

Ataberk
  • 557
  • 1
  • 6
  • 26
  • You can use `app:elevation="xdp"` – AskNilesh Mar 16 '19 at 10:57
  • I didnt get what do you mean but updated the question. You can check it. – Ataberk Mar 16 '19 at 11:01
  • Unsupported XML attributes are simply ignored (assuming they exist in later versions of course). You don't need to wrap it – Zoe Mar 16 '19 at 11:11
  • Yeah actually im not good at the basic of XML, just know what is going on. Question is about "can we make a similar expression?" and "does Android have something like this on its roadmap?" – Ataberk Mar 16 '19 at 11:16

2 Answers2

1

Yes you can do that by adding tool target API:

First add: <RootTag xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" >

Example:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:targetApi="14" >

or by name: tools:targetApi="jelly_bean"

If you want your layout directries to be use in different versions, name your files as:

/res/layout/layout.xml - (default directries)

/res/layout-v14/layout.xml

/res/layout-v17/layout.xml

Also, if you want to dynamically create element in your code:

You can also use annotations in your java code to make things easy:

First import: import android.annotation.TargetApi;

Then, use this annotation above your method:

@TargetApi(Build.VERSION_CODES.HONEYCOMB)

There are more annotation that you can get help:

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)

Above annotation to warn for methods that are used lower API level. Read more about requiresApi: Android API level annotation for Android libraries

Now, inside your method you can dynamically generate views.

Example from the doc:

private void setUpActionBar() {
    // Make sure we're running on Honeycomb or higher to use ActionBar APIs
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
}

Read doc for more details about annotations: https://developer.android.com/studio/write/annotations

Blasanka
  • 21,001
  • 12
  • 102
  • 104
  • Yea actually tools:targetApi="x" look like what am I looking for. But when I tetst targetApi="21" on SDK 17 device, that widget still display in that device. Don't worry made a little search but still didn't get what am i missing. – Ataberk Mar 16 '19 at 18:28
0

You can not give your view elevation in your XML and check for your SDK version in your code - if it's over 21 give the view elevation programmatically.

For example:

 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
    //the API level is above 21 and you can manipulate your view with all the features that available to API level 21+
 }
Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
  • My question wasn't about the elevation. It was about the productivity if its the true word. – Ataberk Mar 16 '19 at 11:04
  • Can you explain again please, what do you mean by "if its the true word"? And in my answer i didnt answered spesipic for elavation, i did it generaly for checking if tye phone api level is above 21 – Tamir Abutbul Mar 16 '19 at 11:06
  • Yeah actually you are right. I updated my question. – Ataberk Mar 16 '19 at 11:13