How to make the first view of a ConstraintLayout fits remaining space Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to make the first view of a ConstraintLayout fits remaining space without wasting too much if your time.

The question is published on by Tutorial Guruji team.

So I have a constraint layout containing 3 text views.These 3 text views are aligned to the left so it gives this schema:

Schema What I want is this: Text views B and C should never ellipsize, only the text view A should if there is not enough space for these 3 to fit inside the constraint layout. Since it’s the first view of the layout, how to achieve that ?

Thank you !

Answer

Try this. You should use app:layout_constraintWidth_max="wrap" and app:layout_constrainedWidth="true" for text view A

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:padding="12dp">

    <TextView
        android:id="@+id/tv_A"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        app:layout_constrainedWidth="true"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/tv_B"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_max="wrap"
        app:layout_goneMarginStart="0dp"
        tools:text="Very long long long long long long long long long text" />

    <TextView
        android:id="@+id/tv_B"
        android:layout_width="wrap_content"
        android:layout_marginStart="12dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toStartOf="@+id/tv_C"
        app:layout_constraintStart_toEndOf="@+id/tv_A"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="Text B" />

    <TextView
        android:id="@+id/tv_C"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="12dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/tv_B"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="Text C" />

</androidx.constraintlayout.widget.ConstraintLayout>
We are here to answer your question about How to make the first view of a ConstraintLayout fits remaining space - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji