How to show ellipses in my TextView if it is more than 1 line?

I have the following layout that does not work:

<LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:id="@+id/experienceLayout" android:background="#ffffff" android:layout_height="match_parent" android:paddingLeft="6dp" android:paddingRight="6dp" android:paddingBottom="6dp" android:paddingTop="6dp"> <TextView android:layout_weight="1" android:id="@+id/experienceLabel" android:text="Experience" android:layout_height="wrap_content" android:textColor="#000000" android:layout_width="wrap_content" android:textStyle="bold"> </TextView> <TextView android:id="@+id/experienceTextView" android:text="TextView" android:layout_height="wrap_content" android:textColor="#000000" android:layout_width="wrap_content" android:ellipsize="end" android:lines="1" android:maxLines="1" android:singleLine="true" android:fadeScrollbars="false"> </TextView> </LinearLayout> 
+89
java android textview android-textview android-linearlayout
Jun 18 2018-11-11T00:
source share
6 answers

This is a common problem. Try using the following:

 android:scrollHorizontally="true" android:ellipsize="end" android:maxLines="1" 

.............. scrollHorizontally is a "special sauce" that makes it work.

+267
Jun 18 2018-11-11T00:
source share

It will also make one line with an ellipsis.

  android:singleLine="true" 
+31
Dec 21 '12 at 10:22
source share

Use this

 android:ellipsize="end" android:singleLine="true" 



Do not use this without knowing fully what comes out

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

When you use maxlines = 1 this will maxlines = 1 truncate most characters.

+19
Feb 22 '16 at 15:26
source share

The way it worked for me on multiple devices / APIs was programmatically similar to this (where tv is your TextView):

  if (tv.getLineCount() > 1) { int lineEndIndex = tv.getLayout().getLineEnd(0); String text = tv.getText().subSequence(0, lineEndIndex - 3) + "\u2026"; tv.setText(text); } 
+12
Sep 18 '15 at 21:30
source share

Thus, all of the answers above satisfy the requirement that only 1 line is displayed and then an ellipsis. However, if you want the ellipsis to appear after certain lines of text, you should use the following:

 android:ellipsize="end" android:maxLines="2" android:singleLine="false" 

In this case, the ellipsis appears only after 2 lines. Note. It is important that singleLine is false.

+3
Jan 24 '19 at 4:34
source share

It helped me:

 android:ellipsize="end" android:maxLines="1" android:singleLine="true" 

Make sure TextView width set to Match_Parent

https://github.com/chrisjenx/Calligraphy/issues/43#issuecomment-523701518

+1
Sep 03 '19 at 19:15
source share



All Articles