How do I use getMeasuredWidth()
and getMeasuredHeight()
? It always returns 0. What is the difference between this and getHeight()
and getWidth()
?

- 7,297
- 6
- 49
- 95

- 11,367
- 11
- 50
- 60
-
Why can't I use `Display display = getWindowManager().getDefaultDisplay();` within a View? – Rohith Nandakumar Nov 02 '10 at 04:53
-
Not a duplicate. Nothing in the question mentions parent dimensions at all! – SMBiggs Jul 29 '19 at 20:04
6 Answers
Just got a solution to get height and width of a custom view:
@Override
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld){
super.onSizeChanged(xNew, yNew, xOld, yOld);
viewWidth = xNew;
viewHeight = yNew;
}
Its working in my case.

- 6,579
- 8
- 53
- 86

- 127,700
- 71
- 241
- 295
-
4I believe this is the correct method to determine width and height. It's also the simplest. – D-Dᴙum Aug 16 '13 at 11:32
-
4
Don't try to get them inside its constructor. Try Call them in onDraw()
method.

- 53,910
- 52
- 193
- 240

- 145
- 2
-
16isn't onDraw called continuously? Any action in there might be negative towards the performance. – ericosg Dec 17 '13 at 10:48
-
1Indeed, I think best practice is to listen to the onsizechanged event, and put the width and height in member variables at that time. You can then use them in your ondraw method without having to ask them again. – David Jan 29 '15 at 19:53
-
1don't do anything heavy in onDraw method. onDraw is for drawing only – nutella_eater Sep 06 '19 at 10:08
You can use the following method to get the width and height of the view, For example,
int height = yourView.getLayoutParams().height;
int width = yourView.getLayoutParams().width;
This gives the converted value of the view which specified in the XML layout.
Say if the specified value for height is 53dp in XML, you will get the converted value in integer as 80.
-
27Note that this will give the width / height as long as a width or height is specified. If you set WRAP_CONTENT, this is not going to give a correct width / height. It will return -1, or 0, depending if its "MATCH_PARENT" or "WRAP_CONTENT". http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html – Reinherd Oct 07 '13 at 07:35
-
True, It's give me -1, instead of actual height of scrollview, I need a Scrollview height, in my case I have scrollview and seekbar, need to change it at same time, i.e If I scroll x amount via scrollview at same time seekbar progress should change same as scrollview. – Hitesh Dhamshaniya Dec 26 '13 at 10:48
-
-
I was also lost around getMeasuredWidth()
and getMeasuredHeight()
getHeight()
and getWidth()
for a long time.......... later i found onSizeChanged()
method to be REALLY helpful.
New Blog Post: how to get width and height dimensions of a customView (extends View) in Android http://syedrakibalhasan.blogspot.com/2011/02/how-to-get-width-and-height-dimensions.html

- 7,425
- 2
- 36
- 44

- 12,376
- 16
- 77
- 113
The difference between getHeight()
and getMeasuredHeight()
is that first method will return actual height of the View
, the second one will return summary height of View
's children. In ohter words, getHeight()
returns view height, getMeasuredHeight()
returns height which this view needs to show all it's elements
-
It can be same at any case ? I got both value same, is it possible ? – Hitesh Dhamshaniya Dec 26 '13 at 10:49
-
Sure, it can be the same. For example, if you child has the same height as its parent. – OFFmind Jan 11 '14 at 07:11
Well getHeight
gets the height, and getWidth
gets the width. But you're calling those methods too soon.
If you're calling them in onCreate
or onResume
, the view isn't drawn yet. You have to call them after the view has been drawn.
-
I was asking the difference between getMeasuredHeight() and getHeight(). Ok how do I get the screen width and height then? – Rohith Nandakumar Nov 02 '10 at 04:48