7

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?

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
to_sam
  • 695
  • 8
  • 19

2 Answers2

14

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

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
10

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

Michael Osofsky
  • 11,429
  • 16
  • 68
  • 113
  • 1
    This works indeed. It is not a requirement of the TextView and its ellipsize tag, but as the [ConstraintLayout documentation](https://developer.android.com/training/constraint-layout/#adjust-the-view-size) points out, if you specify "0dp" the View will "match constraints". – Miloš Černilovský Feb 05 '19 at 15:14