When are View.getWidth and getHeight values valid?

2516 views
Skip to first unread message

revcom

unread,
Mar 4, 2008, 4:47:14 PM3/4/08
to Android Developers
Hi there,

When an activity starts the main content view is selected in
onCreate(0 using "setContentView(R.layout.main);" or similar.
Obviously at this point the specified view (and its nested views) have
not yet been laid out.

Is there a specific Activity.onXXX() override where you can guarantee
all nested views have been laid out and their getWidth() and
getHeight() methods will return the correct values?

Thanks...

Robert

joebowbeer

unread,
Mar 4, 2008, 5:31:28 PM3/4/08
to Android Developers
The view's size is assigned when view.onSizeChanged is called with
oldw = oldh = 0.

Assuming your view is visible, its size has been assigned by the time
the activity's window receives focus. That is, when
activity.onWindowFocusChanged is called with hasFocus = true.

The sequence is roughly:

view.onFinishInflate
view.onMeasure
view.onSizeChanged(width, height, 0, 0)
view.onLayout
activity.onWindowFocusChanged(true)

--
Joe Bowbeer

revcom

unread,
Mar 4, 2008, 5:36:26 PM3/4/08
to Android Developers
Thanks Joe,

Robert
Reply all
Reply to author
Forward
0 new messages