Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: measureChildWithMargins returns different results than view.measure

I'm extending a ViewGroup in Android. Currently, just for test, I add only 1 child view to this view group - a TextView instantiated via code.

in the ViewGroup's onMeasure(), when I call child.measure(), the child measures its height as the entire screen height, even though when I create it, I pass WRAP_CONTENT as the height layout params. However, when I measure the child using measureChildWithMargins, it returns the correct height. Why is this?

public class CustomViewGroup extends ViewGroup {
    public CustomViewGroup (Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        // create header text view
        addView(createTitleHeader(context));
    }

    private TextView createTitleHeader(Context context) {
        TextView result = new TextView(context);
        result.setLayoutParams(new MarginLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        result.setBackgroundColor(Color.RED);
        return result;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));

        for (int i = 0; i < getChildCount(); i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                // the following line scales the TextView's height to the entire screen
                child.measure(widthMeasureSpec, heightMeasureSpec);
                //...while the following line works perfectly
                measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
            }
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        if (changed) {
            // get view group measurements
            final int viewLeft = getPaddingLeft();
            final int viewTop = getPaddingTop();
            final int viewRight = right - left - getPaddingRight();
            final int viewBottom = bottom - top - getPaddingBottom();

            // temp rect used for measuring views
            final Rect rect = new Rect();
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                if (child.getVisibility() != GONE) {
                    // get child params
                    final LayoutParams lp = child.getLayoutParams();
                    final int childWidth = child.getMeasuredWidth();
                    final int childHeight = child.getMeasuredHeight();

                    // calculate rect
                    rect.left = viewLeft;// + lp.leftMargin;
                    rect.top = viewTop;// + lp.topMargin;
                    rect.right = viewLeft + childWidth;// - lp.rightMargin;
                    rect.bottom = viewTop + childHeight;

                    // layout
                    child.layout(rect.left, rect.top, rect.right, rect.bottom);
                }
            }
        }
    }
}
like image 797
user884248 Avatar asked Feb 04 '26 18:02

user884248


1 Answers

Sad that no one answered your question. You must call ViewGroup.measureChild or ViewGroup.measureChildWith Margins; first. These are what initialize the size of your views.

Sadly I have the opposite problem. ViewGroup.measureChildWith Margins causes a crash in my app.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!