Search code examples
androidtextviewandroid-constraintlayouttruncationellipsize

How to truncate Android TextView with margins?


I am attempting to implement the layout shown below:

Layout 0001

i.e. 2 rows of text information. Each row has a label (static text) for the first column, then dynamic info text displayed afterwards. Additionally, the second row will have a small image (same height/width as per the text size) which should be appended to the end of the info text.

The difficulty comes when I attempt to truncate the info sections when they are too large for the screens. I can successfully use the combination below:

android:layout_width="wrap_content"
android:maxLines="1"
android:ellipsize="end"
    

which will add "..." if the text would spill outside of the parent view. But for the second view, I would like to always leave space so that the image is always displayed, and in such a way that the image is anchored to the end of the info text without a gap (i.e. not fully to the right)

e.g.:

Truncated correctly

and

Image anchored to text label

I can't get this to work though - the text always continues to the end of the layout and the image is not displayed, i.e.:

No truncation

The only way I can fix this is by hardcoding a maxWidth, but obviously I need this to work for a variety of screen dimensions.

Current code below (including the maxWidth setting in place):

<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">

    <!-- Txt Label 1-->
    <TextView
        android:id="@+id/txtLabel1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Txt Label 1:"
        android:textColor="@android:color/darker_gray"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <!-- Txt Info 1-->
    <TextView
        android:id="@+id/txtInfo1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:maxLines="1"
        android:ellipsize="end"
        android:textColor="@android:color/darker_gray"
        app:layout_constraintStart_toEndOf="@+id/txtLabel1"
        app:layout_constraintTop_toTopOf="parent" />

    <!-- Txt Label 2-->
    <TextView
        android:id="@+id/txtLabel2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Txt Label 2:"
        android:textColor="@android:color/darker_gray"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtLabel1" />

    <!-- Txt Info 2-->
    <TextView
        android:id="@+id/txtInfo2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxWidth="240dp"
        android:layout_marginStart="8dp"
        android:maxLines="1"
        android:ellipsize="end"
        android:textColor="@android:color/darker_gray"
        app:layout_constraintStart_toEndOf="@+id/txtLabel1"
        app:layout_constraintTop_toBottomOf="@+id/txtLabel1" />

    <!-- Img 1-->
    <ImageView
        android:id="@+id/img1"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginStart="8dp"
        app:layout_constraintStart_toEndOf="@+id/txtInfo2"
        app:layout_constraintTop_toBottomOf="@+id/txtLabel1" />

</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • <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="wrap_content">
    
    <!-- Txt Label 1-->
    <TextView
        android:id="@+id/txtLabel1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Txt Label 1:"
        android:textColor="@android:color/darker_gray"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    <!-- Txt Info 1-->
    <TextView
        android:id="@+id/txtInfo1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="Lorem ipsum dolor sit amet"
        android:textColor="@android:color/darker_gray"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/txtLabel1"
        app:layout_constraintTop_toTopOf="parent" />
    
    <!-- Txt Label 2-->
    <TextView
        android:id="@+id/txtLabel2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Txt Label 2:"
        android:textColor="@android:color/darker_gray"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtLabel1" />
    
    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/txtLabel1"
        app:layout_constraintTop_toBottomOf="@+id/txtLabel1">
        <!-- Txt Info 2-->
        <TextView
            android:id="@+id/txtInfo2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="30dp"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="Lorem ipsum dolor sit amet, consetetur sadipscing elitr"
            android:textColor="@android:color/darker_gray" />
    
        <!-- Img 1-->
        <ImageView
            android:id="@+id/img1"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginStart="-30dp"
            android:layout_toEndOf="@id/txtInfo2"
            android:src="@android:drawable/btn_plus" />
    </RelativeLayout></androidx.constraintlayout.widget.ConstraintLayout>