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

88,377

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.

Share:
88,377

Related videos on Youtube

Sheehan Alam
Author by

Sheehan Alam

iOS, Android and Mac Developer. i can divide by zero.

Updated on July 08, 2022

Comments

  • Sheehan Alam
    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
    Sebastian Contreras almost 12 years
    Strange... I tried android:scrollHorizontally="true" but it didn't work, I had to use the deprecated attribute android:singleLine="true".
  • Oliver Pearmain
    Oliver Pearmain about 10 years
    This is deprecated apparently
  • bpiec
    bpiec about 9 years
    This one better breaks long words.
  • Mariano Zorrilla
    Mariano Zorrilla over 8 years
    This is the most helpful answer of all... works on every single API and can be easily convert it as a Utils library.
  • Chris Stillwell
    Chris Stillwell over 7 years
    You should use the ellipsis character \u2026 instead of the three . characters
  • Marilia
    Marilia over 7 years
    You are right @ChrisStillwell and I do use the ellipsis character in my code. I've edited the answer, thank you. :)
  • Rishabh Dutt Sharma
    Rishabh Dutt Sharma over 7 years
    yes scrollHorizontally is the actual key to ...
  • filthy_wizard
    filthy_wizard about 7 years
    scrollHorizontally? obviously i don't want a horizontal scroll effect.
  • BonanzaDriver
    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
    Jaydev about 7 years
    android:maxLines="1"
  • Spikatrix
    Spikatrix almost 6 years
    @grebulon It is deprecated. At least now.
  • Vaishnav Mhetre
    Vaishnav Mhetre over 5 years
    Using ellipsize with maxLines might crash the app - Report from Android Studio intellisense
  • Pierre
    Pierre about 5 years
    The default value is false according to the documentation: developer.android.com/reference/android/widget/…
  • Chen Li Yong
    Chen Li Yong almost 5 years
    @VaishnavMhetre I just implement the solution above and has no crash at all.
  • jegadeesh
    jegadeesh about 4 years
    Works without scrollHorizontally property.
  • cesards
    cesards about 4 years
    Not true. Documentation doesn't say anything about deprecation: developer.android.com/reference/android/widget/…
  • Reaz Murshed
    Reaz Murshed about 4 years
    @cesards Interesting. This document says that it is - developer.android.com/reference/android/R.attr.html#singleLi‌​ne
  • Neckster
    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).