Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

measuring a view before rendering it

Tags:

android

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?

like image 301
nourdine Avatar asked May 13 '10 13:05

nourdine


People also ask

How do you measure a view?

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.

What's the measurement in Android for views?

Each View has two different types of sizes: the measured width and height, which is basically how big it wants to be, and the width and height that are the actual dimensions they have after being laid out. The desired size cannot always be provided, e.g. a view could request a width larger than its parent's width.


1 Answers

If you want to get the width and height before your activity has added it to its view hierarchy, measured and layed it out you will have to call measure() on the View yourself before calling getMeasuredWidth() or getMeasuredHeight().

As the measured width and height are set after measure has been called (which in turn calls onMeasure()) they will return 0 before this point. You will have to supply MeasureSpecs to measure(..) that will vary depending on your needs.

The MeasureSpecs that allow the children to impose any constraints themselves look like

int widthMeasureSpec = MeasureSpec.makeMeasureSpec(*some width*, MeasureSpec.EXACTLY); int heightMeasureSpec = MeasureSpec.makeMeasureSpec(*some height*, MeasureSpec.EXACTLY); 

Important point. (has been changed in new API) The width passed in above can either be explicit px or ViewGroup.LayoutParams.WRAP_CONTENT or ViewGroup.LayoutParams.FILL_PARENT.

Its also worth noting when your view is measured in the actual destination hierarchy the MeasureSpec that will be passed to measure() will be configured based upon the containing ViewGroups layout logic. It may be called more than once if the first measure vals are not valid when considered in the context of this parent viewGroup / its own layout constraits / the constraints of any sibling child-views. Without waiting for the viewGroup to call measure() before implementing any logic dependent on the measured size it would be hard (depending on the situation) to get the final measuredWidth & height from the above solution, but in all the instances i have used the above technique it has fit the purpose, but they have been relatively simple. If you really do need to measure in context you should probably just do it after onLayout() has returned and explicitly set any changes then requestLayout() again.

like image 78
Dori Avatar answered Oct 11 '22 19:10

Dori