My own experience with OnGlobalLayoutListener is that it does not always wait until it leaves the game. So, if you are looking for the sizes of a certain child (or child, etc.) of the View, then if you check them in the onGlobalLayout () method, you may find that they reflect some intermediate state of the process layout, and not the final (resting ) the state that will be displayed to the user.
When testing this, I noticed that by doing postDelayed () with a delay of one second (obviously a race condition, but it was only for testing), I got the correct value (for a child viewing children), but if I made a simple entry () without delay, I received the wrong value.
My solution was to avoid the OnGlobalLayoutListener and instead use the direct override of dispatchDraw () in my top-level view (the one that contains the child whose child I needed to measure). By default, dispatchDraw () draws all the child views of the current view, so if you send a call to your code that takes measurements after calling super.dispatchDraw (canvas) in dispatchDraw (), you can be sure that all the children are running before measurements, images will be made.
Here's what it looks like:
@Override protected void dispatchDraw(Canvas canvas) {
Of course, the drawing will take place before you take these measurements, and it will be too late for this to happen. But if this is not a problem, then this approach, apparently, very reliably provides the final (resting) measurements of representations of descendants.
After you have the necessary measurements, you will want to set the flag at the top of testAndRespondToChildViewMeasurements () so that it returns without any action (or to make a similar test within dispatchDraw () override itself), because, unlike OnGlobalLayoutListener, There is no way to remove this override.