on which view lifecycle event can i check chip.getLayout()?

94 Views Asked by At

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.

1

There are 1 best solutions below

2
On BEST ANSWER

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