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

AndroidAndroid LayoutViewSizeDimensions

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlgfischerView Question on Stackoverflow
Solution 1 - AndroidPrakash NadarView Answer on Stackoverflow