How do I set the android text view to cut any letters that don't fit in a layout
I have several TextViews in a row and I want the 1st TextView to take up all spare space but if the combined length of the TextViews is too long to fit on one line, I want the 1st TextView to be cut off.
As for the spare space I开发者_StackOverflow have set the 1st TextView's layout_width to match parent and layout_weight to 1 which works. I just can't get it to cut off any extra letters so it fits on one row.
What I want is something like the following with 'Text' and 'Long Text'
'Text :data'
'Long T :data'
So 'Long Text' gets cut off to 'Long T' in order to fit.
What I get is
'Text :data'
'Long Text :d'
Thanks!
Give the first (left) TextView a set size (set in relative terms, dp), or use maxWidth for it, and also use ellipsize.
http://developer.android.com/reference/android/widget/TextView.html#attr_android:ellipsize
This is a good answer to your problem: TextView cuts off text when it is long enough
Also, seems similar to this question which may help: Two TextViews side by side, only one to ellipsize?
Try something like this:
android:singleLine="true"
android:ellipsize="start"
android:scrollHorizontally="true"
Try this answer https://stackoverflow.com/a/58497299/2717821 to cut left text if too long
<?xml version="1.0" encoding="utf-8"?>
<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"
android:padding="8dp">
<TextView
android:id="@+id/leftText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
app:layout_constrainedWidth="true"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintEnd_toStartOf="@id/rightText"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="|short destination|" />
<TextView
android:id="@+id/rightText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/leftText"
app:layout_constraintTop_toTopOf="parent"
tools:text="|next departure|" />
</androidx.constraintlayout.widget.ConstraintLayout>
精彩评论