0

I have a Chip view.

I want to check if the text is ellipsized and if so - to replace the text with a shorter text (on run time).

I have seen this code to check if the text in the Chip is ellipsized.

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

But I don't know at what lifecycle event should i call this method, as for this line

Layout l = myAccountView.getLayout();

I get l = null

I have view lifecycle (frame layout that holds my Chip)

I have tried to check on onDraw() and in onLayout()

I have also tried to call from the Dialog that hold the frame

but I know inflation is top to buttom, so it returns l= null on setContentView() as well.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Elad Benda
  • 35,076
  • 87
  • 265
  • 471

1 Answers1

0

Approach 1: Do call myAccountView.onPreDraw(); just before myAccountView.getLayout()

Approach 2 : Using ViewTreeObserver

ViewTreeObserver vtObserver= myAccountView.getViewTreeObserver();
vtObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
       Layout layout = myAccountView.getLayout();  
    }
});
Krishna Sharma
  • 2,828
  • 1
  • 12
  • 23
  • You don't need to call, it's get called by the system. I have given example to solve the issue **either** by using [onPreDraw](https://developer.android.com/reference/android/widget/TextView#onPreDraw()) request a new layout before drawing occurs **OR** `ViewTreeObserver`. I meant to just use any not both. Recommended approach is to use `ViewTreeObserver ` – Krishna Sharma Aug 02 '18 at 11:34
  • thanks, i understand it's one or the other. I'm just curious to understand why `myAccountView.onPreDraw()` is needed to make my layout not null. it worked. just want to understand why is this call explicitly needed. – Elad Benda Aug 05 '18 at 09:51