I have the following Layout which does not work:
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.
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);
}
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.
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.
This helped me out:
android:ellipsize="end"
android:maxLines="1"
android:singleLine="true"
Make sure the TextView
width
is set to Match_Parent
https://github.com/chrisjenx/Calligraphy/issues/43#issuecomment-523701518
This will also make a single line with ellipsise
android:singleLine="true"