How to set maxLines and ellipsis TextView at the same time

I want to limit my text view to a maximum of 5 lines, so I did:

<TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:maxLines="5" /> 

But when I try to configure it to add "..." when the text is truncated, I add android: ellipsize = "end". I see ... but then my TextView has only the maximum row of 2, instead of 5.

Can you suggest how I can make the textual representation of the maximum line 5 and add "..." when it is truncated?

Thank.

+44
android
May 26 '10 at a.m.
source share
5 answers

Pro-army you can use:

 your_text_view.setEllipsize(TextUtils.TruncateAt.END); your_text_view.setMaxLines(4); your_text_view.setText("text"); 
+47
Apr 23 '13 at
source share

try this code ...

  final TextView tv_yourtext = (TextView)findViewById(R.id.text); tv_yourtext.setText("A really long text"); ViewTreeObserver vto = tv_yourtext.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { ViewTreeObserver obs = tv_yourtext.getViewTreeObserver(); obs.removeGlobalOnLayoutListener(this); if(tv_yourtext.getLineCount() > 6){ Log.d("","Line["+tv_yourtext.getLineCount()+"]"+tv_yourtext.getText()); int lineEndIndex = tv_yourtext.getLayout().getLineEnd(5); String text = tv_yourtext.getText().subSequence(0, lineEndIndex-3)+"..."; tv_yourtext.setText(text); Log.d("","NewText:"+text); } } }); 
+26
Aug 08 '12 at 6:45
source share

You only need to add this line to your TextView:

 android:ellipsize="marquee" 
+7
Mar 29 '13 at 1:22
source share

There is the same question android ellipsize multiline textview This is a known bug, quite old and not yet fixed :(

someone posted a workaround http://code.google.com/p/android-textview-multiline-ellipse/ maybe this will help you (or someone else)

+6
Feb 16 '11 at 12:25
source share

When I set the height of the TextView to a constant value (e.g. 50dp), it works fine for me.

0
Feb 10 '17 at 19:39
source share



All Articles