Leonardo Simas
Leonardo Simas

Reputation: 71

Using <include> nested in <merge> tag - Android

I have two reusable layouts, header_item.xml and custom_view.xml (using merge tag). The custom_view is always used inside a LinearLayout tag.

I want to include the header_item inside this custom_view but the Android Studio gives me this error message: "Cannot resolve class include".

The strange thing is that I can run the app and it seems to work fine. But I am not sure if it is an Android Studio issue or I shouldn't be using include inside a merge tag.

Here are the xml files:

header_item:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="80dp"
        android:layout_height="80dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:text="Header"
        android:textStyle="bold"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

and the custom_view:

<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="wrap_content"
    android:orientation="vertical"
    tools:parentTag="android.widget.LinearLayout">

    <include layout="@layout/header_item" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text from custom view" />

</merge>

Upvotes: 6

Views: 1941

Answers (2)

Dmitriy Pavlukhin
Dmitriy Pavlukhin

Reputation: 419

<include> inside of <merge> works absolutely fine, however Android Studio cannot handle this situation and will show Cannot resolve class include error on the tag. Maybe it will be fixed in the future.

Upvotes: 7

zhangxaochen
zhangxaochen

Reputation: 34017

merge is not a view, see here.

so, change the custom_view like following:

<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:parentTag="android.widget.LinearLayout">

        <include layout="@layout/header_item" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="text from custom view" />

    </LinearLayout>

</merge>

Upvotes: 2

Related Questions