How can I show ellipses on my TextView if it is greater than the 1 line?

JavaAndroidAndroid LinearlayoutTextview

Java Problem Overview


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>

Java Solutions


Solution 1 - Java

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 - Java

This will also make a single line with ellipsise

 android:singleLine="true"

Solution 3 - Java

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 - Java

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 - Java

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.

Solution 6 - Java

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

Solution 7 - Java

android:singleLine is deprecated. In my case, I had to get a fixed height for the TextView and I used the android:lines attribute instead of android:maxLines. I thought this might help someone having the same problem as mine.

android:ellipsize="end"
android:lines="2"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSheehan AlamView Question on Stackoverflow
Solution 1 - JavaBonanzaDriverView Answer on Stackoverflow
Solution 2 - JavaAtul BhardwajView Answer on Stackoverflow
Solution 3 - JavaMohamed IbrahimView Answer on Stackoverflow
Solution 4 - JavaMariliaView Answer on Stackoverflow
Solution 5 - JavaSDK4551View Answer on Stackoverflow
Solution 6 - JavaEdward MotloungView Answer on Stackoverflow
Solution 7 - JavaReaz MurshedView Answer on Stackoverflow