67

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.

Karu
  • 4,512
  • 4
  • 30
  • 31
michael
  • 106,540
  • 116
  • 246
  • 346

7 Answers7

62

Progrmatically, you can use:

your_text_view.setEllipsize(TextUtils.TruncateAt.END);
your_text_view.setMaxLines(4);
your_text_view.setText("text");  
Imeshke
  • 811
  • 6
  • 13
27

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);
            }

        }
    });
Manoj Manani
  • 507
  • 4
  • 9
  • 1
    This code works perfectly for me! @michael, if it works for you, you should mark it as the answer. – neevek Nov 06 '12 at 04:36
  • I get a question. what is the function of the following code. ViewTreeObserver obs = tv_yourtext.getViewTreeObserver(); obs.removeGlobalOnLayoutListener(this); C an I use vto.removeGlobalOnLayoutListener(this) instead? Thanks. – RxRead Nov 08 '13 at 08:06
  • using `RecyclerView.Adapter TextView` not set text check this my question https://stackoverflow.com/questions/60790803/using-viewtreeobserver-i-am-adding-setmaxlines-and-setellipsize-in-materialtextv – Kishan Viramgama Mar 23 '20 at 10:12
22

Apparently you have to specify the inputType and set it to none in this case. It should look something like this:

<TextView
  android:ellipsize="end"
  android:inputType="none"
  android:maxLines="2"
SeptimusX75
  • 2,155
  • 1
  • 15
  • 10
13

You only need to add this line to your TextView:

android:ellipsize="marquee"
John Alexander Betts
  • 4,718
  • 8
  • 47
  • 72
6

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

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

Community
  • 1
  • 1
Lope
  • 5,388
  • 4
  • 30
  • 40
2
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"  
android:maxLines="5"
android:ellipsize="end" />

Works for me, make sure TextView top container should have height like

android:layout_height="wrap_content"
0

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

david72
  • 7,151
  • 3
  • 37
  • 59