Search code examples
androidxmlandroid-linearlayout

Constrain two items to top and bottom inside linear layout


I have a constraint layout and inside an image and a linear layout. The linear layout contains two text views which I would like to appear on top and bottom of the linear layout constrains:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/chatLayout"
    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/chatPictureIV"
        android:layout_width="@dimen/chat_image_size"
        android:layout_height="@dimen/chat_image_size"
        android:scaleType="centerInside"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="@dimen/chat_image_size"
        android:orientation="vertical"
        app:layout_constraintStart_toEndOf="@+id/chatPictureIV"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/chatNameTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Top"
            android:layout_gravity="top"
            android:textStyle="bold"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/chatStatusTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:text="Bottom"
            android:textSize="18sp" />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

However the second text view appears indeed underneath the first one, but not constrained to the bottom of the linear layout:

enter image description here

How can I constrain the bottom text to the bottom of the linear layout?


Solution

  • This is the case where a RelativeLayout is more suitable.
    All you have to do is set the attributes android:layout_alignParentTop and android:layout_alignParentBottom to true for the 2 views:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/chatLayout"
        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/chatPictureIV"
            android:layout_width="@dimen/chat_image_size"
            android:layout_height="@dimen/chat_image_size"
            android:scaleType="centerInside"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="@dimen/chat_image_size"
            app:layout_constraintStart_toEndOf="@+id/chatPictureIV"
            app:layout_constraintTop_toTopOf="parent">
    
            <TextView
                android:id="@+id/chatNameTV"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Top"
                android:layout_alignParentTop="true"
                android:textStyle="bold"
                android:textSize="18sp" />
    
            <TextView
                android:id="@+id/chatStatusTV"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:text="Bottom"
                android:textSize="18sp" />
    
        </RelativeLayout>
    
    </androidx.constraintlayout.widget.ConstraintLayout>