Search code examples
androidimageviewandroid-linearlayoutandroid-imageviewandroid-custom-view

Programmatically Adding ImageViews in LinearLayout


I'm Programmatically Creating Simple LinearLayout which has two Child(Imageviews) occupies the equal width, In this case the Rootview is CustomLayout(SquareLinearLayout).

Here is the SquareLinearLayout :

public class SquareLinearLayout extends LinearLayout {

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

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int orientation = getContext().getResources().getConfiguration().orientation;

        if (orientation == Configuration.ORIENTATION_PORTRAIT || orientation == Configuration.ORIENTATION_UNDEFINED) {

            int width = MeasureSpec.getSize(widthMeasureSpec);
            int height = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
            setMeasuredDimension(width, height);

        } else {

            int height = MeasureSpec.getSize(heightMeasureSpec);
            int width = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
            setMeasuredDimension(width, height);

        }

    }
}

Related XML

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <com.customviews.SquareLinearLayout
        android:id="@+id/lay_square"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ececec"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/lay_r_capture"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">
        </LinearLayout>

    </com.customviews.SquareLinearLayout>

</LinearLayout>

Programmatically Adding the ImageView in LinearLayout

lay_r_capture = (LinearLayout)findViewById(R.id.lay_r_capture);
LinearLayout.LayoutParams layparam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
lay_r_capture.setOrientation(LinearLayout.HORIZONTAL);
lay_r_capture.setWeightSum(2f);
lay_r_capture.setLayoutParams(layparam);

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT, 1f);
    lp.setMargins(8,8,8,8);

ImageView img1 = new ImageView(this);
Bitmap photo1 = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.photo_1);
img1.setLayoutParams(lp);
img1.setImageBitmap(photo1);

ImageView img2 = new ImageView(this);
Bitmap photo2 = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.photo_2);
img2.setLayoutParams(lp);
img2.setImageBitmap(photo2);

lay_r_capture.addView(img1);
lay_r_capture.addView(img2);

This Things not working as expected like this

Output :

enter image description here

Which Attributes or LayoutParams i'm missing ?


Solution

  • You are Programatically setting MATCH_PARENT as height here :

    lay_r_capture = (LinearLayout)findViewById(R.id.lay_r_capture);
    LinearLayout.LayoutParams layparam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    lay_r_capture.setOrientation(LinearLayout.HORIZONTAL);
    lay_r_capture.setWeightSum(2f);
    lay_r_capture.setLayoutParams(layparam);
    

    So change it to WRAP_CONTENT like :

    lay_r_capture = (LinearLayout)findViewById(R.id.lay_r_capture);
    LinearLayout.LayoutParams layparam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    lay_r_capture.setOrientation(LinearLayout.HORIZONTAL);
    lay_r_capture.setWeightSum(2f);
    lay_r_capture.setLayoutParams(layparam);