0

I have problem to check, if text view been ellipsized. I defined layout for item in recycle view and I have to check, if text was ellipsized and hide button if yes. I found solution, where can I can get layout from text view and check if it was ellipsized, but in bind method in recycle view it always return false. Do you have someone some idea, how I can do it?

Layout l = textview.getLayout();
 if (l != null) {
 int lines = l.getLineCount();
if (lines > 0)
    if (l.getEllipsisCount(lines-1) > 0)
        Log.d(TAG, "Text is ellipsized");
}

This code is not working for me.

SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87

3 Answers3

1

try this in your adapter

    holder.textView.post(new Runnable() {
            @Override
            public void run() {
                if (holder.textView.getLayout() != null) {
                    if (widthText == 0) {
                        widthText = holder.textView.getWidth();
                    }
                    boolean isEllipsize = !holder.textView.getText().toString().equalsIgnoreCase(holder.textView.getLayout().getText().toString());
                } else {
                    Paint paint = new Paint();
                    paint.setTextSize(holder.textView.getTextSize());
                    final float size = paint.measureText(holder.textView.getText().toString());
                    boolean isEllipsize = (int) (size / maxLine) > widthText;
                }
            }
        });

if you want to read more if text too long you can use this libs: https://github.com/bravoborja/ReadMoreTextView

hstung
  • 31
  • 5
0

As stated in another SO post:

This only works after the layout phase, otherwise the returned layout will be null, so call this at an appropriate place in your code.

Make sure this is called after the text had been laid out (After onCreate)!

bkach
  • 1,431
  • 1
  • 15
  • 24
0

Try this,

  <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:lines="1" />
Santhosh
  • 160
  • 7