My original problem was needing to know the height and width of my root View
so that I could make programmatic layout changes. For my purposes, I don't necessarily need to determine this information during onCreate()
; it's quite sufficient for me to programmatically add my child View
s after layout of the root has completed, so therefore I'm happy to use onWindowFocusChanged()
as a hook to determine when calling the root View
's getWidth()
and getHeight()
will return valid results.
However, I see quite a few people have asked how to determine the root View
's size during onCreate()
. I'm guessing people want to do this for better user experience - perhaps so that users don't see the layout being built in stages (if anyone can clarify the reasons, I'd appreciate it). Now, the answer I have commonly seen given to this, such as the one here, is to post a Runnable
to the root View
within onCreate()
. Inside that Runnable
we perform the getWidth()
, etc. The reason why this works is because, apparently, Runnable
objects posted to a View
are executed when the View
has layout and / or is attached to the window.
This brings me to my question. In the API documentation for View
, it doesn't seem to describe this behavior. So, can anyone tell me where it is defined and documented? Or, is it a matter of inspecting the source? Is it an absolutely and rigidly defined feature that Runnables
will stay in a View
's queue and only be handled at a certain point after the layout process?
Further clarification: On reading Kerry's answer and thinking it over a bit more, my basic question can be clarified as follows: In the answer given here and also in this CodeProject entry, I understand that we get around the problem of the root View's
dimensions not being available during the onCreate()
by posting a Runnable
to the View
. As Kerry points out, the guarantee is that this message Runnable
cannot be executed until onCreate()
has executed. I understand that layout occurs some point after onCreate
, but I still can't grasp at the moment why this Runnable
should execute when the View
's dimensions are known.