What is the difference between getWidth/Height() and getMeasuredWidth/Height() in Android SDK?
Asked Answered
H

1

110

The Android Documentation says that there is two sizes for a view, the measured dimensions and the drawing dimensions. The measured dimension is the one computed in the measure pass (the onMeasure method), while the drawing dimensions are the actual size on screen. Particularly, the documentation says that:

These values may, but do not have to, be different from the measured width and height.

So, my question is: what could make the drawing dimension be different of the measured dimension? If the onMeasure(int,int) method respects the layout requirements (given as the parameters widthMeasureSpec and heightMeasureSpec, how could the SDK decides that the view should have a different drawing size?

Additionally, how/where in the Android Source Code the measured width/height is used to compute the drawing width/height? I tryed to look into the View source code, but I can't figure out how the measuredWidth/Height is used to compute the final width/height. Maybe it has something to do with the padding, but I'm not sure.

Hyperbole answered 28/12, 2011 at 15:8 Comment(3)
I think that these measures can be different in case if a scroll is added to the view.Archine
Maybe, but I developed a custom ViewGroup, and this is happening in my code. I am not dealing with scroll until the point where this behavior occurs. So I think that there should be more than scroll involved into this.Hyperbole
I confirm, I developer a custom ViewGroup too to force a given aspect-ratio: I forced measured width and height (lower or equal then proposed), but I had as result real width and height not changed (not the measured ones). No relation with scroll. I really cannot find a way to force the real width and height.Codeine
C
115

As the name suggests the measuredWidth/height is used during measuring and layoutting phase.

Let me give an example,

A widget is asked to measure itself, The widget says that it wants to be 200px by 200px. This is measuredWidth/height.

During the layout phase, i.e. in onLayout method. The method can use the measuredWidth/height of its children or assign a new width/height by calling layout method of the view.

lets say the onLayout method calls childview.layout(0,0,150,150) now the width/height of the view is different than the measured width/height.

I would suggest not to use the measuredWidth/height outside onLayout method.

to summarize .

  1. onMeasure -> sets up measuredWidth/measuredHeight
  2. onLayout -> sets up the width/height of the widget.

additionallly
public void View.layout(int l, int t, int r, int b)
seems to be place where the assignment of position and size happens.

Cassino answered 29/12, 2011 at 6:10 Comment(7)
that is , we should always use getWidth/getHeight to get the "real" dimension of a View, right?Copp
Yes, but if you are doing your own layouting then you should use measuredWidth instead of width.Cassino
So, what's the purpose of setting dimesions inside onMeasure()? I mean, the layout pass is the last one, in which you set the final dimensions of the view, possibliy regardless of measured dimensionsPurcell
@Purcell it depends upto the layout (LinearLayout, RelativeLayout or your custom layout) , to give the correct size and position to the view.Cassino
Ok let's say the view tells "I'd like these dimensions". While the layout says "You will have these dimensions". Is it right?Purcell
@Purcell yes. But with so many customviews that I have implemented, the measuredwidth being different than width has been rare. But you should avoid using the incorrect width. measuredWidth maynot always be same as width.Cassino
Hi, I am creating a canvas. For the width / height, should I pass in the measured width / height or just say getWidth() / getHeight(). Also, why shouldn't you use measuredWidth / measuredHeight outside of the onLayout() method? Thanks.Escutcheon

© 2022 - 2024 — McMap. All rights reserved.