125

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>
JJD
  • 50,076
  • 60
  • 203
  • 339
Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556

7 Answers7

339

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.

BonanzaDriver
  • 6,411
  • 5
  • 32
  • 35
38

This will also make a single line with ellipsise

 android:singleLine="true"
Atul Bhardwaj
  • 6,647
  • 5
  • 45
  • 63
25

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.

Paul Chu
  • 1,249
  • 3
  • 19
  • 27
Mohamed Ibrahim
  • 841
  • 11
  • 9
18

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);
    }
Marilia
  • 1,911
  • 1
  • 19
  • 28
4

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.

SDK4551
  • 109
  • 6
  • The default value is false according to the documentation: https://developer.android.com/reference/android/widget/TextView#attr_android:singleLine – Pierre Mar 06 '19 at 06:59
3

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

Kiran Maniya
  • 8,453
  • 9
  • 58
  • 81
2

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"
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • Not true. Documentation doesn't say anything about deprecation: https://developer.android.com/reference/android/widget/TextView#attr_android:singleLine – cesards Apr 15 '20 at 01:19
  • 1
    @cesards Interesting. This document says that it is - https://developer.android.com/reference/android/R.attr.html#singleLine – Reaz Murshed Apr 15 '20 at 01:24
  • 1
    @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). – Neckster Oct 25 '21 at 13:35