package org.kulabukhov.android.commons.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import org.kulabukhov.android.commons.R;

/**
 * Created by admin on 01/06/14.
 */
public class BoundedLinearLayout extends LinearLayout {

    private final int mBoundedWidth;

    private final int mBoundedHeight;

    public BoundedLinearLayout(Context context) {
        super(context);
        mBoundedWidth = 0;
        mBoundedHeight = 0;
    }

    public BoundedLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.BoundedLinearLayout);
        mBoundedWidth = a.getDimensionPixelSize(R.styleable.BoundedLinearLayout_bounded_width, 0);
        mBoundedHeight = a.getDimensionPixelSize(R.styleable.BoundedLinearLayout_bounded_height, 0);
        a.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // Adjust width as necessary
        int measuredWidth = MeasureSpec.getSize(widthMeasureSpec);
        if(mBoundedWidth > 0 && mBoundedWidth < measuredWidth) {
            int measureMode = MeasureSpec.getMode(widthMeasureSpec);
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(mBoundedWidth, measureMode);
        }
        // Adjust height as necessary
        int measuredHeight = MeasureSpec.getSize(heightMeasureSpec);
        if(mBoundedHeight > 0 && mBoundedHeight < measuredHeight) {
            int measureMode = MeasureSpec.getMode(heightMeasureSpec);
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(mBoundedHeight, measureMode);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

@@ -340,38 +340,32 @@
 %0D%0A%0D%0A    private 
-final 
 int mBoundedWidt
@@ -386,14 +386,8 @@
 ate 
-final 
 int 
@@ -1838,12 +1838,244 @@
 %0D%0A    %7D%0D
+%0A%0D%0A%09public void setBoundedWidth(int boundedWidth) %7B%0D%0A%09%09this.mBoundedWidth = boundedWidth;%0D%0A%09%09requestLayout();%0D%0A%09%7D%0D%0A%0D%0A%09public void setBoundedHeight(int boundedHeight) %7B%0D%0A%09%09this.mBoundedHeight = boundedHeight;%0D%0A%09%09requestLayout();%0D%0A%09%7D%0D
 %0A%7D%0D%0A

change BoundedLinearLayout bounded width and height at runtime