How can I show ellipses on my TextView if it is greater than the 1 line?
Solution 1
This is a common problem. Try using the following:
android:scrollHorizontally="true"
android:ellipsize="end"
android:maxLines="1"
.............. the scrollHorizontally is the "special sauce" that makes it work.
Solution 2
This will also make a single line with ellipsise
android:singleLine="true"
Solution 3
Use this
android:ellipsize="end"
android:singleLine="true"
Don't use this without fully aware of what output comes
android:ellipsize="end"
android:maxLines="1"
When you use maxlines = 1
it will some time truncate most of the characters.
Solution 4
The way it worked for me on multiple devices / APIs was programmatically like 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);
}
Solution 5
So all the answers above cater to the requirement that only 1 line and then the ellipsis should appear. However if you want the ellipsis to appear after certain lines of text, then you should use the following:
android:ellipsize="end"
android:maxLines="2"
android:singleLine="false"
With this the ellipsis will appear only after 2 lines. Note: Its important to have singleLine as false.
Related videos on Youtube
Sheehan Alam
iOS, Android and Mac Developer. i can divide by zero.
Updated on July 08, 2022Comments
-
Sheehan Alam almost 2 years
I have the following Layout which 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>
-
Sebastian Contreras almost 12 yearsStrange... I tried
android:scrollHorizontally="true"
but it didn't work, I had to use the deprecated attributeandroid:singleLine="true"
. -
Oliver Pearmain about 10 yearsThis is deprecated apparently
-
bpiec about 9 yearsThis one better breaks long words.
-
Mariano Zorrilla over 8 yearsThis is the most helpful answer of all... works on every single API and can be easily convert it as a Utils library.
-
Chris Stillwell over 7 yearsYou should use the ellipsis character
\u2026
instead of the three.
characters -
Marilia over 7 yearsYou are right @ChrisStillwell and I do use the ellipsis character in my code. I've edited the answer, thank you. :)
-
Rishabh Dutt Sharma over 7 yearsyes
scrollHorizontally
is the actual key to...
-
filthy_wizard about 7 yearsscrollHorizontally? obviously i don't want a horizontal scroll effect.
-
BonanzaDriver about 7 years@user1232726: Yes, "scroll horizontally." As a matter of common sense, take a look at the date of the question and the date of the responses ... goes without saying that neither might be currently relavant (before commenting).
-
Jaydev about 7 yearsandroid:maxLines="1"
-
Spikatrix almost 6 years@grebulon It is deprecated. At least now.
-
Vaishnav Mhetre over 5 yearsUsing ellipsize with maxLines might crash the app - Report from Android Studio intellisense
-
Pierre about 5 yearsThe default value is false according to the documentation: developer.android.com/reference/android/widget/…
-
Chen Li Yong almost 5 years@VaishnavMhetre I just implement the solution above and has no crash at all.
-
jegadeesh about 4 yearsWorks without scrollHorizontally property.
-
cesards about 4 yearsNot true. Documentation doesn't say anything about deprecation: developer.android.com/reference/android/widget/…
-
Reaz Murshed about 4 years@cesards Interesting. This document says that it is - developer.android.com/reference/android/R.attr.html#singleLine
-
Neckster over 2 years@cesards, According to Android in Code documentation: This (android:singleLine) attribute is deprecated. Use maxLines instead to change the layout of a static text, and use the textMultiLine flag in the inputType attribute instead for editable text views (if both singleLine and inputType are supplied, the inputType flags will override the value of singleLine).