androidandroid-layoutandroid-constraintlayoutandroid-include

Id on <include> tag destroys layout


I have a layout in a separate xml file which gets included in other files. I want to reference the included file so I set an id. But with the id the layout gets completely unstructured. Mini example:

Parent-Layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

</android.support.constraint.ConstraintLayout>

Included layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/constraint_parent"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/t1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="1"
    app:layout_constraintEnd_toStartOf="@id/t2"
    app:layout_constraintStart_toStartOf="@id/constraint_parent" />

<TextView
    android:id="@+id/t2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="2"
    app:layout_constraintEnd_toStartOf="@id/t3"
    app:layout_constraintStart_toEndOf="@id/t1" />

<TextView
    android:id="@+id/t3"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="3"
    app:layout_constraintStart_toEndOf="@id/t2"
    app:layout_constraintEnd_toEndOf="@id/constraint_parent"/>

The result is following layout: Include without id

But if I change the include tag to following:

<include
    android:id="@+id/test"
    layout="@layout/test_include" />

The result is: Include with id

So the layout gets lost completely. Is it not possible to add an id to an include tag? I want to add the include tag two times, thats why I want to add two different ids to the two include instead of referencing the parent layout of the included layout directly.


Solution

  • the problem in the included layout, please use this xml instead

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/constraint_parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <TextView
        android:id="@+id/t1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toStartOf="@id/t2"
        app:layout_constraintStart_toStartOf="parent" />
    
     <TextView
        android:id="@+id/t2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="2"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toStartOf="@id/t3"
        app:layout_constraintStart_toEndOf="@id/t1" />
    
     <TextView
        android:id="@+id/t3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="3"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toEndOf="@id/t2"
        app:layout_constraintEnd_toEndOf="parent"/>
     </android.support.constraint.ConstraintLayout>