View's getWidth() and getHeight() returns 0
The correct View
dimensions are fetched post layout completion. If getWidth()
and getHeight()
run prematurely, they return 0 due to the unfinished layout. To get the dimensions after layout finalization use a ViewTreeObserver.OnGlobalLayoutListener
:
Dimensions in lifecycle phases
If the lifecycle methods like onStart()
or onResume()
are called, it's a good time to get the view dimensions. In such events, layout process is expected to be complete. Specifically, during animations, use dimensions from onWindowFocusChanged
for reliable size tracking.
Animations in the spotlight:
Customizing view handling
Creating custom views? onLayout
is the method to capture width & height. Although, keep in mind that onLayout
is a diva: it appears multiple times on stage throughout the lifecycle.
For static view configurations
If the layout is static and unchanging, getMeasuredWidth()
and getMeasuredHeight()
should be your go-to tools. These offer the size the view intends to be, even when the layout hasn't come out of the dressing room yet.
Valid view sizing assurance
To ensure if your view has gone through its makeover and flaunts proper width and height values, check ViewCompat.isLaidOut(view)
.
To lineup a task on the view's message queue, which gets executed post layout, use View.post(Runnable):
Consistency across contexts
For maintaining consistent styling and sizing across various UI and to avoid hard-coded dimension values that may not account for varying screen sizes and densities, use dimension resources while inflating your custom UIs.
Effective animation use
Realize the difference between visible size during animations and actual interactive area of a view. For touch events, actual area is key. Make sure animated transformations don't ruin user's expectations by reducing touch target sizes unexpectedly.
Was this article helpful?