Reputation: 494
I'm trying to make a specific textview truncate itself when it takes up the maximum space. Here is an example of the layout situation I have:
|| [TextView 1] [TextView 2] [ImageView] ||
This horizontal chain exists in a cell that has a static width. The first TextView has static text and the image view icon has a static size, while the second textview has variable width. I chained them together and set the constraints appropriately.
My problem is that when the TextView 2 gets too long, it pushes the ImageView out of the cell even though the ImageView's right constraint is set to the parent. What I want to happen is that when the TextView 2 gets too long, it should truncate, leaving the icon within the bounds of the parent. Is there a way I can make the left edge of the ImageView act as a boundary that the TextView 2 must respect when the ImageView reaches the end of the parent?
Edit:
Here's a barebones example of my xml. I think it is also worth noting that the xml of the layout below is being injected into another layout using the include tag. The parent of the xml below has its dimensions set to match-parent
, and the layout it is being injected into sets the dimensions to a static width and height.
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
bind:layout_constraintLeft_toLeftOf="parent"
bind:layout_constraintRight_toLeftOf="@id/info"
bind:layout_constraintBottom_toBottomOf="parent"
bind:layout_constraintHorizontal_chainStyle="packed"/>
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:text="@{viewModel.info}"
bind:layout_constraintLeft_toRightOf="@id/title"
bind:layout_constraintRight_toLeftOf="@id/icon"
bind:layout_constraintBottom_toBottomOf="@id/title"/>
<ImageView
android:id="@+id/icon"
android:layout_width="12dp"
android:layout_height="12dp"
android:src="@drawable/icon"
android:layout_marginLeft="4dp"
bind:layout_constraintLeft_toRightOf="@id/info"
bind:layout_constraintBottom_toBottomOf="@id/info"
bind:layout_constraintTop_toTopOf="@id/info"
bind:layout_constraintRight_toRightOf="parent"/>
Upvotes: 1
Views: 1920
Reputation: 6346
Set app:layout_constrainedWidth="true"
attribute for second TextView
. This will enforce constraints even when the TextView's
width is wrap_content
.
If you also want it to stay within one line and ellipsize at the end when maximum width is reached, add:
android:maxLines="1"
android:ellipsize="end"
Upvotes: 3