Does posting Runnable to an UI thread guarantee that layout is finished when it is run?

I like the question too. It forced me to dig into Android source code once again. I believe this works because post() gets called after setContentView().

Method setContentView() ends up in calling ViewGroup.addView() of the top view, and addView() call always triggers requestLayout(). In turn, requestLayout() posts a task to the main thread to be executed later. This task will execute measure and layout on the view hierarchy. Now if you post another task it will be put into the queue after layout task and, as the result, always executed after measure and layout happen. Thus you will always have valid sizes.

Leave a Comment