PROBLEM: Suppose we have simple case: we have view and we have to show some data on this view. We use static method showData(View view) to do this. I want to know the exact moment when view layout is measured and I can access getWidth() getHeight() and be sure that this is final width and height of my view.
WHAT I KNOW: (not sure I am 100% right):
I know 3 different approaches to do this
- view.addOnLayoutChangeListener - we know when layout is changed, we can get height and width
- view.getViewTreeObserver().addOnGlobalLayoutListener - for my case almost the same, but we get information later. Because layoutChange bubbles from children to root, and globalLayout from root to children (right?).
- view.post(Runnable r) - is working, but I do not know why runnable is posted to ui thread executes after view is measured.
QUESTION: What is the best approach to know that view is measured? and why?