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

Solution 1:

Progrmatically, you can use:

your_text_view.setEllipsize(TextUtils.TruncateAt.END);
your_text_view.setMaxLines(4);
your_text_view.setText("text");  

Solution 2:

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

        }
    });

Solution 3:

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"

Solution 4:

You only need to add this line to your TextView:

android:ellipsize="marquee"