Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

views

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()?

like image 504
Rohith Nandakumar Avatar asked Nov 02 '10 04:11

Rohith Nandakumar


People also ask

How to get height in custom view Android?

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.

What are two different ways to set the height and width of component in Android?

First get layout params using view. getLayoutParams(). Second then set the height and width value then last set the layout params of view.


2 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.

like image 83
Paresh Mayani Avatar answered Oct 04 '22 14:10

Paresh Mayani


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

like image 40
Mozz Avatar answered Oct 04 '22 13:10

Mozz