0

I would like to do something like this...

Two TextViews, first multiline and on the end i need another textview

|t1 content t1 content t1 content     |
|t1 content t1 content... [t2 content]|

With shorter content

|t1 content t1 content t1 content     |
|t1 content               [t2 content]|

It is similar to the question anserwed here Two TextViews side by side, only one to ellipsize? ... but i need multiline solution

Any ideas?

Community
  • 1
  • 1

1 Answers1

0

Try this

I have created this global function in which you have to pass textview object and no of lines after you want to ellipse end.

public void doEllipsize(final TextView tv, final int maxLine) {
        ViewTreeObserver vto = tv.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

            @SuppressWarnings("deprecation")
            @Override
            public void onGlobalLayout() {

                ViewTreeObserver obs = tv.getViewTreeObserver();
                obs.removeGlobalOnLayoutListener(this);
                if (maxLine <= 0) {
                    int lineEndIndex = tv.getLayout().getLineEnd(0);
                    String text = tv.getText().subSequence(0, lineEndIndex - 3) + "...";
                    tv.setText(text);
                } else if (tv.getLineCount() >= maxLine) {
                    int lineEndIndex = tv.getLayout().getLineEnd(maxLine - 1);
                    String text = tv.getText().subSequence(0, lineEndIndex - 3) + "...";
                    tv.setText(text);
                }
            }
        });
    }
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
  • its not exactly problem with elipsize but with layout ex. i am creating RelativeLayout... one textview is multiline and is parent aligned top/left. second textview is short and aligned bottom/right. problem appears when first textvie have long text - it is elipsized right at second line becouse have attribute lines="2" but its under the second textvie... i need to elipsize on a second line but shorter than the width of this second textview maybe there is any xml (layout) solution for that case – Grzegorz Pawełczuk Sep 05 '13 at 07:42