71

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

JonasVautherin
  • 7,297
  • 6
  • 49
  • 95
Rohith Nandakumar
  • 11,367
  • 11
  • 50
  • 60

6 Answers6

74

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.

Ziem
  • 6,579
  • 8
  • 53
  • 86
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
12

Don't try to get them inside its constructor. Try Call them in onDraw() method.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
Mozz
  • 145
  • 2
  • 16
    isn't onDraw called continuously? Any action in there might be negative towards the performance. – ericosg Dec 17 '13 at 10:48
  • 1
    Indeed, 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
  • 1
    don't do anything heavy in onDraw method. onDraw is for drawing only – nutella_eater Sep 06 '19 at 10:08
12

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.

Ziem
  • 6,579
  • 8
  • 53
  • 86
Sam
  • 555
  • 4
  • 13
  • 27
    Note 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
  • doesnt work in init of custom view getLayoutParams comes null – JSONParser Nov 11 '18 at 13:59
  • on inherited view class this method(getLayoutParams) return null. – mehdi Jun 24 '19 at 14:18
10

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

Praful Bhatnagar
  • 7,425
  • 2
  • 36
  • 44
Rakib
  • 12,376
  • 16
  • 77
  • 113
6

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

biegleux
  • 13,179
  • 11
  • 45
  • 52
OFFmind
  • 597
  • 1
  • 5
  • 13
1

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.

xarlymg89
  • 2,552
  • 2
  • 27
  • 41
Falmarri
  • 47,727
  • 41
  • 151
  • 191