Android: How to get a custom View's height and width?

132,877

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

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

Solution 5

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

Share:
132,877
Rohith Nandakumar
Author by

Rohith Nandakumar

iOS | Android | Java Web Developer | #SOreadytohelp

Updated on April 08, 2021

Comments

  • Rohith Nandakumar
    Rohith Nandakumar about 3 years

    Possible Duplicate:
    How to size an Android view based on its parent’s dimensions

    how do I use getMeasuredWidth() and getMeasuredHeight? It always returns 0. what is the diffrence between this and getHeight() and getWidth()?

    • Rohith Nandakumar
      Rohith Nandakumar over 13 years
      Why can't I use Display display = getWindowManager().getDefaultDisplay(); within a View?
    • SMBiggs
      SMBiggs almost 5 years
      Not a duplicate. Nothing in the question mentions parent dimensions at all!
  • Rohith Nandakumar
    Rohith Nandakumar over 13 years
    I was asking the difference between getMeasuredHeight() and getHeight(). Ok how do I get the screen width and height then?
  • D-Dᴙum
    D-Dᴙum almost 11 years
    I believe this is the correct method to determine width and height. It's also the simplest.
  • Reinherd
    Reinherd over 10 years
    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". developer.android.com/reference/android/view/…
  • ericosg
    ericosg over 10 years
    isn't onDraw called continuously? Any action in there might be negative towards the performance.
  • Hitesh Dhamshaniya
    Hitesh Dhamshaniya over 10 years
    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
    Hitesh Dhamshaniya over 10 years
    It can be same at any case ? I got both value same, is it possible ?
  • OFFmind
    OFFmind over 10 years
    Sure, it can be the same. For example, if you child has the same height as its parent.
  • David
    David over 9 years
    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.
  • droid kid
    droid kid over 9 years
    one problem is while keyboard comes up this gets called
  • JSONParser
    JSONParser over 5 years
    doesnt work in init of custom view getLayoutParams comes null
  • mehdi
    mehdi about 5 years
    on inherited view class this method(getLayoutParams) return null.
  • nutella_eater
    nutella_eater almost 5 years
    don't do anything heavy in onDraw method. onDraw is for drawing only