28

I have a TextView set to ellipsize but when I put the text in it doesn't do so. Any ideas?

<TextView 
    android:id="@+id/LegalsView" 
    android:ellipsize="end"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium" />
mrsus
  • 5,446
  • 4
  • 31
  • 44
digipen79
  • 359
  • 2
  • 4
  • 11

13 Answers13

64

Try:

android:singleLine="true"
Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
Edwin Evans
  • 2,726
  • 5
  • 34
  • 47
17
android:layout_width="wrap_content"

will allow the TextView to expand as long as it needs to (including running off the screen). To make it ellipsize, you're going to have to set a width as citizen conn recommended, preferably with android:layout_width="fill_parent" instead of an absolute value.

Additional hints: You'll also want to set the maxLines of the TextView (probably to 1), and to get the actual ellipsis ("...") to appear, you'll probably also have to set

android:scrollHorizontally="true"
Glendon Trullinger
  • 4,052
  • 1
  • 28
  • 35
  • Glendon, shouldn't `scrollHorizontally` be `false` if you want to do `android:ellipsize="end"`. (It should be `true` if you want to do `android:ellipsize="marquee"`.) Also, I find adding the `android:lines` or `android:maxLines` attribute to be sufficient - no need for `android:scrollHorizontally`. Oh and settings `lines` or `maxLines` to be greater than `1` works for me also. – Adil Hussain Aug 01 '12 at 13:23
  • When I posted this answer over a year ago, this was correct, and scrollHorizontally needed to be set to true in order to get the ellipsize attribute to work. I don't do much Android development anymore, so if this has changed, feel free to propose an edit. – Glendon Trullinger Aug 03 '12 at 16:07
  • 2
    No, it still has to be true otherwise the ellipsis will not show. Makes no sense but there you go. – zeh Dec 18 '12 at 01:33
12

You need to match the settings for android can correctly calculate the position of the object. Has several possible combinations, and depends on the rest of the layout is on the same line.

This is a combination that works:

android:layout_width="0dip" // dynamic width
android:layout_weight="1"   // less weight, other objects still "0"
android:ellipsize="end"     // ... into end of text (right)
android:singleLine="true"   // one line, deprecated, but necessary for some ambients
android:maxLines="1"        // one line, new mode

In summary, you indicate the weight, if you use dynamic layout, or just a fixed size, then indicates to only have one line and configures how "..." is displayed.

Rtisatto
  • 767
  • 8
  • 7
9

My app started ignoring maxlines & ellipsize when I added

android:textIsSelectable="true"

It seems like these options are not compatible together.

WigglyWorld
  • 705
  • 6
  • 13
  • 1
    WigglyWorld @nivm I too face this issue. How did you solve this? – Anju Feb 25 '19 at 09:13
  • @Mathew ive found out that if you change ellipsize to "end" or basically any thing that is not "middle" it's working. i didnt found out any smooth solution when using "middle" type with android:textIsSelectable.. – nivm Feb 25 '19 at 09:26
  • @Mathew make sure you use - singleLine="true" – nivm Feb 25 '19 at 09:31
  • I made my textview a custom class which cannot be scrolled. Used it with android:textIsSelectable = "true" & it worked. public class NoScrollTextView extends AppCompatTextView { public NoScrollTextView(Context context) { super(context); } public NoScrollTextView(Context context, AttributeSet attrs) { super(context, attrs); } public NoScrollTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public void scrollTo(int x, int y) { //do nothing } } – Anju Feb 25 '19 at 09:56
5

The only way I've been able to make this work is to set width="1dp", weight="1", ellipsize="end", maxLines="1" and scrollHorizontally="true". The first two have the same sort of effect as width="match_parent" without the widget expanding off the edge of the display, the remaining three settings all had to be made to get the text to ellipsize.

blahdiblah
  • 33,069
  • 21
  • 98
  • 152
R.L.Watkins
  • 51
  • 1
  • 1
3

If you are trying to get multiline ellipsis working, unfortunately it doesn't work very well. This is a known bug in the Android platform and as far as I know it hasn't been fixed.
In a TextView the best you can get is 2 lines ellipsizing (which can be achieved by setting

android:maxLines="2"

Refer to this link.
Even with an absolute width as mentioned in another answer, it still only gives you at max, 2 lines of ellipsis. (But as mentioned also, single line ellipsis is achievable). So for example, your text might fill your 8 line TextView, but will look like this when you turn ellipsis on:

+---------------------------------------------------+
|                                                   |
| This is my cool text that is supposed to fill the |
| entire textview but unfortunately because of t... |
|                                                   |
|                                                   |
|                                                   |
|                                                   |
|                                                   |
|                                                   |
|                                                   |
|                                                   |
|                                                   |
|                                                   |
|                                                   |
|                                                   |
|                                                   |
+---------------------------------------------------+

If you know the size of your TextView, you can use a custom component available here (Unfortunately the google code project that originally hosted it seems to have disappeared, hence this link is all I could find).

Community
  • 1
  • 1
Caspar Harmer
  • 8,097
  • 2
  • 42
  • 39
2

This solution works:

android:ellipsize="end"
android:singleLine="false"
android:lines="3"
android:maxLines="3"

Width can also be set to "wrap_content"

2

If you aren't defining a specific width for your TextView the text will ellipsize first when your text is reaching the parent view's width.

Wroclai
  • 26,835
  • 7
  • 76
  • 67
0

You change your layout_width to certain dp instead of wrap_content

and

use
android:singleLine="true"

android:ellipsize="end"

code:

    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:text="check this text"
    android:id="@+id/textView"
    android:singleLine="true"
    android:ellipsize="end"/>
Anandu
  • 145
  • 1
  • 4
0

Only works runtime, do something like this:

textView.setEllipsize(TruncateAt.END);
textView.setLines(1);
textView.setHorizontallyScrolling(true);
textView.setText("Long text goes here");
Egemen Hamutçu
  • 1,602
  • 3
  • 22
  • 34
0

You can as well set maxLines like so:

        <TextView
            android:id="@+id/txt_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="3"
            android:ellipsize="end"
            android:textStyle="bold"/>

Ellipses appear when the text exceeds the maxLines set.

Cletus Ajibade
  • 1,550
  • 15
  • 14
0

android:maxWidth="150dp" was the only instance when text ellipsized for me.

iCantC
  • 2,852
  • 1
  • 19
  • 34
-1

I think you need to specify an absolute width for it to work.

android:layout_width="100dip"

or

android:layout_width="fill_parent"
citizen conn
  • 15,300
  • 3
  • 58
  • 80