0

I'm trying to dynamically add items to the my CustomView and make the final height the size of all the children added together. The only problem is the child.getMeasuredHeight or child.getMeasuredWidth always returns a value of 0 during run time. When I'm debugging it will randomly 1 out of 10 times seem to actually contain the data I was actually expecting with a value of 192. If I also hard code a value into the layout parameter instead of using WRAP_CONTENT it still shows a value of 0. Is there something that I'm doing wrong.

xml file

<CustomLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_marginTop="80dp"
    android:orientation="vertical"
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:id="@+id/custom_layout"
    android:background="@drawable/custom_shape"/>
</CustomLayout>

Here's a part of my CustomLayout.java class

public class CustomLayout extends LinearLayout {

public CustomLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomLayout(Context context) {
    super(context);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int count = getChildCount();

    int maxHeight = 0;
    int maxWidth = 0;

    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    for(int i=0; i <count; i++) {
        final View child = getChildAt(i);
        if(child.getVisibility() != GONE) {

            maxHeight +=  child.getMeasuredHeight();

        }
    }

    setMeasuredDimension(widthSize,maxHeight);
}

In a part of my main activity

 Button b = new Button(this);
        b.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        b.setText("Sample Test");
        b.setTextSize(14);
        mCustomView.addView(b);
AConsiglio
  • 256
  • 1
  • 3
  • 13

1 Answers1

0

You will need to ask each child of the ViewGroup to measure itself before you can access its dimensions. This is done with a call to [measureChild](https://developer.android.coma/reference/android/view/ViewGroup.html#measureChild(android.view.View, int, int)) or [measureChildWithMargins](https://developer.android.coma/reference/android/view/ViewGroup.html#measureChild(android.view.View, int, int)).

Take a look at the developer guide for ViewGroup to see how the child measurements are obtained.

// Iterate through all children, measuring them and computing our dimensions
// from their size.
for (int i = 0; i < count; i++) {
    final View child = getChildAt(i);
    if (child.getVisibility() != GONE) {
        // Measure the child.
        measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);

        // Update our size information based on the layout params.  Children
        // that asked to be positioned on the left or right go in those gutters.
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
        if (lp.position == LayoutParams.POSITION_LEFT) {
            mLeftWidth += Math.max(maxWidth,
                    child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
        } else if (lp.position == LayoutParams.POSITION_RIGHT) {
            mRightWidth += Math.max(maxWidth,
                    child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
        } else {
            maxWidth = Math.max(maxWidth,
                    child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
        }
        maxHeight = Math.max(maxHeight,
                child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
        childState = combineMeasuredStates(childState, child.getMeasuredState());
    }
}
Cheticamp
  • 61,413
  • 10
  • 78
  • 131