Search code examples
androidandroid-constraintlayout

Android constraint layout textview to wrap context and show ellipse if no more space


I want to use a constraint layout that will be able to satisfy

(1) wrap the content of 2nd TextView enter image description here

(2) extend the width of the 2nd TextView until it shows ellipses when there are no more spaces. enter image description here

All these 3 TextViews are on the same line and the background colours are much needed.

<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="match_parent"
    android:background="@android:color/white"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:background="@android:color/darker_gray"
        android:padding="4dp"
        android:text="Text View 1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:background="@android:color/holo_green_light"
        android:ellipsize="end"
        android:gravity="fill"
        android:maxLines="1"
        android:padding="4dp"
        android:text="Short Text "
        app:layout_constraintBottom_toBottomOf="@+id/text1"
        
        app:layout_constraintStart_toEndOf="@+id/text1"
        app:layout_constraintTop_toTopOf="@+id/text1" />

    <TextView
        android:id="@+id/text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:background="@android:color/holo_orange_dark"
        android:padding="4dp"
        android:text="Text View 3"
        android:textAlignment="center"
        app:layout_constraintBottom_toBottomOf="@+id/text1"
        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintTop_toTopOf="@+id/text1" />

</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • Constrain the middle text view to the last and use layout_constrainedWidth="true" with the width set to wrap_content instead of 0dp, as well as setting layout_constraintHorizontal_bias="0.0" so it aligns to the first view.

    Short text, Long text

    <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="match_parent"
        android:background="@android:color/white">
    
    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:background="@android:color/darker_gray"
        android:padding="4dp"
        android:text="Text View 1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:background="@android:color/holo_green_light"
        android:ellipsize="end"
        android:maxLines="1"
        android:padding="4dp"
        android:text="Short text"
        app:layout_constrainedWidth="true"
        app:layout_constraintBottom_toBottomOf="@+id/text1"
        app:layout_constraintEnd_toStartOf="@+id/text3"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/text1"
        app:layout_constraintTop_toTopOf="@+id/text1" />
    
    <TextView
        android:id="@+id/text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:background="@android:color/holo_orange_dark"
        android:padding="4dp"
        android:text="Text View 3"
        android:textAlignment="center"
        app:layout_constraintBottom_toBottomOf="@+id/text1"
        app:layout_constraintEnd_toEndOf="parent"
    
        app:layout_constraintTop_toTopOf="@+id/text1" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>