Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set maxLines and ellipsize of a TextView at the same time

Tags:

android

I want to limit my text view to have 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 do see the ... but then my TextView only has a max line of 2, instead of 5.

Can you please suggest how can I make the text view of maximum line of 5 and add '...' when it get truncated?

Thank you.

like image 839
michael Avatar asked May 26 '10 00:05

michael


People also ask

Can you have multiple styles inside TextView?

Use multiple widgets if you need precise placement of discrete bits of text. Use inline markup if you, um, need markup inline in a widget. Remember: there is no FlowLayout in Android, so stringing together multiple TextViews to create a paragraph is not truly practical AFAIK.

How do I Auto Resize TextView?

To use preset sizes to set up the autosizing of TextView in XML, use the android namespace and set the following attributes: Set the autoSizeText attribute to either none or uniform. none is a default value and uniform lets TextView scale uniformly on horizontal and vertical axes.

How do you check if a TextView's text exceeds the max lines?

text_view. post(new Runnable() { @Override public void run() { if (text_view. getLineCount() < text_view. getMaxLines()) { // do stuff } } });

How do you get 3 dots at the end of a TextView text?

You are applying to your TextView a compound Drawable on the right.. to make the three dots appear in this scenario, you have to apply a android:drawablePadding="{something}dp" attribute to the TextView as well. Hope it helps!


2 Answers

Progrmatically, you can use:

your_text_view.setEllipsize(TextUtils.TruncateAt.END); your_text_view.setMaxLines(4); your_text_view.setText("text");   
like image 174
Imeshke Avatar answered Sep 19 '22 22:09

Imeshke


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);             }          }     }); 
like image 28
Max M. Avatar answered Sep 20 '22 22:09

Max M.