0

I have a layout where just one part of it is api level dependent (it uses vectors) and I need to handle it differently for api-19. I changed the layout to handle those using app:srcCompat:"@drawable/left_arrow_vector" which works to make 19 not crash, but when I test it on other api levels, it still shows the same layout as api-19 (There is a sizing problem which makes it easy to see. I'll fix that when I get this part figured out).

res/layout-v19:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@id/calTitleBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:orientation="horizontal">

    <ImageView
        android:id="@id/prevMonth"
        app:srcCompat="@drawable/left_arrow_vector"
        android:contentDescription="@string/prev_month"
        android:layout_height="24dp"
        android:layout_width="24dp" />

    <TextView
        android:id="@id/calMonth"
        style="@style/CalendarTitle"
        android:layout_toStartOf="@id/nextMonth"
        android:layout_toEndOf="@id/prevMonth"
        tools:text="JANUARY 2019" />

    <ImageView
        android:id="@id/nextMonth"
        android:layout_alignParentRight="true"
        app:srcCompat="@drawable/right_arrow_vector"
        android:contentDescription="@string/next_month"
        android:layout_height="24dp"
        android:layout_width="24dp" />

</RelativeLayout>

res/layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@id/calTitleBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:orientation="horizontal">

    <ImageButton
        android:id="@id/prevMonth"
        style="@style/CalendarLeftArrowButton"
        android:contentDescription="@string/prev_month" />

    <TextView
        android:id="@id/calMonth"
        style="@style/CalendarTitle"
        android:layout_toStartOf="@id/nextMonth"
        android:layout_toEndOf="@id/prevMonth"
        tools:text="JANUARY 2019" />

    <ImageButton
        android:id="@id/nextMonth"
        style="@style/CalendarRightArrowButton"
        android:contentDescription="@string/next_month" />
</RelativeLayout>

relevant part of styles.xml:

    <style name="CalendarArrowButtons">
        <item name="android:layout_width">44dp</item>
        <item name="android:layout_height">44dp</item>
    </style>

    <style name="CalendarLeftArrowButton" parent="CalendarArrowButtons">
        <item name="android:layout_alignParentLeft">true</item>
        <item name="android:background">@drawable/left_arrow_vector</item>
    </style>

    <style name="CalendarRightArrowButton" parent="CalendarArrowButtons">
        <item name="android:layout_alignParentRight">true</item>
        <item name="android:background">@drawable/right_arrow_vector</item>
    </style>

When I remove the include and just use either directly, it displays right (well, the normal layout crashes on 19, but that's to be expected) on the respective versions.

user
  • 29
  • 7

1 Answers1

0

Does <include> respect different api levels such as /layout-v19/layout.xml vs /layout/layout.xml?

Yes, the tag is processed at runtime by LayoutInflater and the referenced layouts are loaded like any other layouts in your resources.

What I believe the problem is that:

  • layout-v19 is applied to API level 19 and above unless there are more specific resource matches
  • layout is the least specific so it only applies to API levels below 19 for you

So if you want another layout for API 20 and above, put it in layout-v20.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • One of those a-Ha moments. – user Jan 30 '19 at 20:10
  • I misread/misunderstood that part about "and above". So the specificity would put `/layout` at the bottom and with a `minSdk=19` it is basically useless. Tried your answer with a `layout-v19` and a `layout-v20` and it worked perfectly. It would be nice if there was a way to tell Android to just "use this layout for just this API" and have `/layouts` be the default if not overridden. Thank you. – user Jan 30 '19 at 20:17