tags:

views:

65

answers:

2

hello

I need to find out how big a view will be after attaching it to its parent.

I have overridden this method:

onMeasure(int, int);

but it looks like this method is invoked only when I actually add my custom view to it's container using:

addView(myView);

Do you think there is a way to get this information before rendering the view itself? Basically I need to know the actuall size before attaching it and not attach the view at all if it would take more certain amount of space.

anybody?

A: 

You should be able to use getMesauredWith() and getMeasuredHeight()

http://developer.android.com/reference/android/view/View.html#getMeasuredWidth%28%29

Roflcoptr
A: 

OnMeasure does not tell you the size of the View. Instead, it asks your custom View to set its size by providing some constraints enforced by the parent View.

This is from the SDK documentation:

The first pair is known as measured width and measured height. These dimensions define how big a view wants to be within its parent (see Layout for more details.) The measured dimensions can be obtained by calling getMeasuredWidth() and getMeasuredHeight().

The second pair is simply known as width and height, or sometimes drawing width and drawing height. These dimensions define the actual size of the view on screen, at drawing time and after layout. These values may, but do not have to, be different from the measured width and height. The width and height can be obtained by calling getWidth() and getHeight().

After layout you can call getWidth() and getHeight() to find the final size of your custom View.

Prashast