Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConstraintLayout does not ellipsize long text in TextView

I have a TextView to the right of an image. I am trying to place some long text next to the image but this text should automatically end by adding "..." at the end. However, this does not work. I use this layout:

<android.support.constraint.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"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp">

<ImageView
    android:id="@+id/file_icon"
    android:layout_width="250px"
    android:layout_height="250px"
    android:layout_gravity="center"
    android:clickable="true"
    android:scaleType="centerInside"
    android:src="@drawable/ic_launcher"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/file_title"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:maxLines="1"
    android:text="This is a very long title and I hope I have the dots to break it"
    android:textAppearance="@style/TextAppearance.AppCompat.Medium"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toTopOf="@+id/file_type"
    app:layout_constraintVertical_chainStyle="packed"
    app:layout_constraintStart_toEndOf="@+id/file_icon"
    app:layout_constraintWidth_default="wrap"/>

<TextView
    android:id="@+id/file_type"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:text="Type"
    android:textAppearance="@style/TextAppearance.AppCompat.Small"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/file_title"
    app:layout_constraintLeft_toRightOf="@+id/file_icon" />

<ImageView
    android:id="@+id/file_download"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_gravity="center"
    android:clickable="true"
    android:scaleType="centerInside"
    android:src="@drawable/ic_action_download"
    app:layout_constraintTop_toBottomOf="@+id/file_title"
    app:layout_constraintRight_toRightOf="parent" />

The result is this:

enter image description here

Why does the long text not ellipsize such that "..." appears at the end? I have read this and this post but it does not work for me. Can anyone help me here?

like image 234
to_sam Avatar asked Apr 26 '18 05:04

to_sam


Video Answer


2 Answers

Use app:layout_constraintEnd_toEndOf="parent" to your file_title TextView

Try this

<android.support.constraint.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"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp">

    <ImageView
        android:id="@+id/file_icon"
        android:layout_width="250px"
        android:layout_height="250px"
        android:layout_gravity="center"
        android:clickable="true"
        android:scaleType="centerInside"
        android:src="@drawable/abc"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/file_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="This is a very long title and I hope I have the dots to break it"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        app:layout_constraintBottom_toTopOf="@+id/file_type"
        app:layout_constraintStart_toEndOf="@+id/file_icon"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed"
        app:layout_constraintEnd_toEndOf="parent"

        />

    <TextView
        android:id="@+id/file_type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Type"
        android:textAppearance="@style/TextAppearance.AppCompat.Small"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/file_icon"
        app:layout_constraintTop_toBottomOf="@+id/file_title" />

    <ImageView
        android:id="@+id/file_download"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:clickable="true"
        android:scaleType="centerInside"
        android:src="@drawable/abc"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/file_title" />

</android.support.constraint.ConstraintLayout>

OUTPUT

enter image description here

like image 60
AskNilesh Avatar answered Oct 05 '22 19:10

AskNilesh


I've often faced the same problem but for a different reason. Apparently when you use ellipsize you have to use android:layout_width="0dp" for the TextView. I've often mistakenly used android:layout_width="wrap_content". The person who posted the original problem didn't make this mistake, but I'm pointing out that it can be another cause of "ConstraintLayout does not ellipsize long text in TextView".

like image 31
Michael Osofsky Avatar answered Oct 05 '22 21:10

Michael Osofsky