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.
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.
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.
text_view. post(new Runnable() { @Override public void run() { if (text_view. getLineCount() < text_view. getMaxLines()) { // do stuff } } });
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!
Progrmatically, you can use:
your_text_view.setEllipsize(TextUtils.TruncateAt.END); your_text_view.setMaxLines(4); your_text_view.setText("text");
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); } } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With