Android add other layout in ConstraintLayout using include tag

AndroidXmlAndroid LayoutLayoutAndroid Constraintlayout

Android Problem Overview


I'm making a simple test app using ConstraintLayout, but I have a problem.

Here is my code:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout 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.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.user.myapplication.activity.MainActivity">

    <Button
        android:id="@+id/btn_launch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="16dp"
        android:text="launch"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/text_view"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_marginEnd="16dp"
        android:layout_marginTop="16dp"
        android:text="Hello World!"
        app:layout_constraintHorizontal_bias="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_launch" />

    <include
        layout="@layout/content_main"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/text_view" />

</android.support.constraint.ConstraintLayout>

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout 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.support.constraint.ConstraintLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="123456"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:text="98765"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:text="abc"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView3" />

</android.support.constraint.ConstraintLayout>

Code result

Problem


I want "content_main" to be under the "Hello world!" TextView.

I using RelativeLayout, LinearLayout, ConstraintLayout at "content_main" element, but they do not not work.

I don't find any solution.

Does the Android "include" tag not work with ConstraintLayout?

Android Solutions


Solution 1 - Android

Android include tag does work with ConstraintLayout, but you need to declare how big is the layout you want to include with the following attributes.

<include
     layout="@layout/content_main"
     android:layout_width="100dp"
     android:layout_height="250dp"
     .../>

For included layout having dynamic height use wrap_content as a value in layout_height or layout_width attributes of include tag.

<include
    android:id="@+id/input_include"
    layout="@layout/task_input"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

After that, your constraints should work.

Solution 2 - Android

In the constrain layout define the height and width and your constraints as follows:

<include
    app:layout_constraintTop_toBottomOf="@id/custom_members_toolbar_layout"
    app:layout_constraintBottom_toTopOf="@id/file_xfer_bottom_nav_bar"
    android:layout_height="0dp"
    android:layout_width="match_parent"
    layout="@layout/file_xfer_upload_layout"/>

This will auto resize the height based on your constraints.

Cheers!

Solution 3 - Android

Not sure if the ideal solution but what worked for me was to have the include tag hosted by its own constraint layout such as

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/todaysFightLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toTopOf="@+id/tvLatestUpdate"
    app:layout_constraintStart_toStartOf="parent">

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

</androidx.constraintlayout.widget.ConstraintLayout>

And then add constraints from this new constraint layout to the next item to be displayed, in my case a textview with id tvLatestUpdate.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBlueBrightView Question on Stackoverflow
Solution 1 - AndroidMaciej BeimcikView Answer on Stackoverflow
Solution 2 - AndroidDerwreckedView Answer on Stackoverflow
Solution 3 - AndroidFrancislainy CamposView Answer on Stackoverflow