ANDROID: Overriding OnMeasure

When generating a custom layout view, you will often need to override the onMeasure method. sometimes, you just want the width or height to 'fill_parent'... in this case we cant simply do:

setMeasuredDimension(fill_parent, fill_parent);

NOR can we pass in the width & height values received by onMeasure like so:

setMeasuredDimension(width, height);

INSTEAD, here's what you need to do:

super.onMeasure(w, h);
setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight());

This will get you the parent's width and height, assuming you've set its layout-params to be (w=fill_parent, h=fill_parent)

No comments: