Android. How to depends on ids of included layout

Viewed 100

I have first layout I want to include

// first_layout.xml
<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="match_parent">

    <TextView
        android:id="@+id/tvFirstLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="First layout text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

And I have second layout, where first will be included

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

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

    <TextView
        android:id="@+id/tvSecondLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text of second layout"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tvFirstLayout"/> // THIS IS THE QUESTION

</androidx.constraintlayout.widget.ConstraintLayout>

Now the problem is use first textView as constraint to second textView. Any resolves of this ?

3 Answers

Give height, width and id to the <include> tag and then you can set the constraints.

You can set id for the include tag

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

<TextView
    android:id="@+id/tvSecondLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text of second layout"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@id/include"/> // Note you can reference include tag with id

and then reference the include tag with its id.

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

Now you are able to set the constraints.

Related