How can I show ellipses on my TextView if it is greater than the 1 line?
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>
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.