Android: How to get a custom View's height and width?
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
Rohith Nandakumar
iOS | Android | Java Web Developer | #SOreadytohelp
Updated on April 08, 2021Comments
-
Rohith Nandakumar about 3 years
Possible Duplicate:
How to size an Android view based on its parent’s dimensionshow do I use
getMeasuredWidth()
andgetMeasuredHeight
? It always returns 0. what is the diffrence between this andgetHeight()
andgetWidth()
?-
Rohith Nandakumar over 13 yearsWhy can't I use
Display display = getWindowManager().getDefaultDisplay();
within a View? -
SMBiggs almost 5 yearsNot a duplicate. Nothing in the question mentions parent dimensions at all!
-
-
Rohith Nandakumar over 13 yearsI was asking the difference between getMeasuredHeight() and getHeight(). Ok how do I get the screen width and height then?
-
D-Dᴙum almost 11 yearsI believe this is the correct method to determine width and height. It's also the simplest.
-
Reinherd over 10 yearsNote 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 over 10 yearsisn't onDraw called continuously? Any action in there might be negative towards the performance.
-
Hitesh Dhamshaniya over 10 yearsTrue, 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 over 10 yearsIt can be same at any case ? I got both value same, is it possible ?
-
OFFmind over 10 yearsSure, it can be the same. For example, if you child has the same height as its parent.
-
David over 9 yearsIndeed, 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 over 9 yearsone problem is while keyboard comes up this gets called
-
JSONParser over 5 yearsdoesnt work in init of custom view getLayoutParams comes null
-
mehdi about 5 yearson inherited view class this method(getLayoutParams) return null.
-
nutella_eater almost 5 yearsdon't do anything heavy in onDraw method. onDraw is for drawing only