Search code examples
androidlistviewgridviewviewgroup

Resize child view based on parent width and height


enter image description here I have create custom view group .

<com...Template
            android:id="@+id/template"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_weight=".5" />

i dynamically adding to above layout on runtime.

Template.java

 currentTemplateView = LayoutInflater.from(context).inflate(R.layout.template_one, this, false);

addView(currentTemplateView, 0);
@Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        Log.e("Onla", "" + l + t);
        int row, col, left, top;
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            child.layout(0, 0, getMeasuredWidth(), getMeasuredHeight());
        }

        // addView(currentTemplateView, 0);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int mode = MeasureSpec.getMode(widthMeasureSpec); // mode == View.MesaureSpec.EXACTLY
        int size = MeasureSpec.getSize(widthMeasureSpec); // size == 400
        Log.e("onmeasure", "Width: " + mode + "," + size);
        for (int i = 0; i < getChildCount(); i++) {
            measureChildWithMargins(getChildAt(i), widthMeasureSpec, 0, heightMeasureSpec, 0);

            // getChildAt(i).measure(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

Normal Layout with height of 289 dp(template). display good..

When i used in List or grid view . Child in view group are collapsed. it should fit to parent view group (template.java).

enter image description here

I want viewgroup as look like First image whatever resize i do .. Let me what i need to change in my code or any idea for this..

Thanks in Advance.


Solution

  • Finally I did what i need for .. So i just scale the template view

         if (isScaleLayout) {
    
                   view.setScaleX(scaleX);/0-1
                    view.setScaleY(scaleY);/0-1
                    view.setPivotX(0f);
                    view.setPivotY(getWidth());
                }
    **XML** 
    
     <FrameLayout
            android:layout_width="150dp" // Actually size for layout/ adapter  view
            android:layout_height="80dp"
            android:padding="3dp">
    
            <com..Template
                android:id="@+id/template"
                android:layout_width="@dimen/template_height"//205
                android:layout_height="@dimen/template_height"//205 />
    
    
        </FrameLayout>