Patrick.SE
Patrick.SE

Reputation: 4564

Custom Layout ignoring layout params

I can't seem to replicate the XML equivalent in code :

XML Layout VS Programmed Layout

Layout code

<GridLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:columnCount="3">

    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_columnSpan="2"
        android:layout_rowSpan="2"
        android:layout_gravity="fill"
        android:src="@drawable/square_outline"/>

    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/square_outline"/>

    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/square_outline"/>

    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/square_outline"/>

    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/square_outline"/>
    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/square_outline"/>
</GridLayout>

Java "equivalent"

public class ImageSelectionGridView extends GridLayout
{
    private static final int SQUARE_SIZE = 10;

    private int mSquareSize;

    private ImageView mMainImageView;
    private ImageView mRemainingImageViews[];

    public ImageSelectionGridView(Context ctx, AttributeSet attrs)
    {
        super(ctx, attrs);

        TypedArray a = ctx.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.ImageSelectionGridView,
                0, 0);

        try
        {
            mSquareSize = a.getDimensionPixelSize(R.styleable.ImageSelectionGridView_squareSize,
                    SQUARE_SIZE);

        } finally
        {
            a.recycle();
        }

        mMainImageView = new ImageView(ctx);
        mMainImageView.setImageDrawable(ctx.getResources().getDrawable(R.drawable.square_outline));
        GridLayout.LayoutParams params = new GridLayout.LayoutParams();
        params.width = params.height = mSquareSize;

        params.columnSpec = GridLayout.spec(0, 2);
        params.rowSpec = GridLayout.spec(0, 2);
        params.setGravity(Gravity.FILL);
        mMainImageView.setLayoutParams(params);

        addView(mMainImageView);

        mRemainingImageViews = new ImageView[5];
        for (int i = 0; i < mRemainingImageViews.length; i++)
        {
            createRemainingImage(ctx);
        }
    }

    private ImageView createRemainingImage(Context ctx)
    {
        ImageView remainingImage = new ImageView(new ContextThemeWrapper(ctx, R.style.EditablePictureStyle));
        remainingImage.setImageDrawable(ctx.getResources().getDrawable(R.drawable.square_outline));
        GridLayout.LayoutParams params = new GridLayout.LayoutParams();
        params.width = params.height = mSquareSize;

        addView(remainingImage);

        return remainingImage;
    }
}

XML for Code with attrs.xml:

<domain.slingbee.ui.ImageSelectionGridView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:columnCount="3"
                        custom:squareSize="70dp"
                        />

<declare-styleable name="ImageSelectionGridView">
        <attr name="squareSize" format="dimension"/>
    </declare-styleable>

For some reason the sizes are different, I'm passing 70dp to the java code and setting all the image views with that specific width and height, why are they different?

Upvotes: 0

Views: 226

Answers (1)

Henry
Henry

Reputation: 17861

Inside your createRemainingImage(), you are creating the layoutParams but not setting them to the ImageView.

    GridLayout.LayoutParams params = new GridLayout.LayoutParams();
    params.width = params.height = mSquareSize;

You just created the params. But you didn't set this to the ImageView. You need this:

    remainingImage.setLayoutParams(params);

Upvotes: 1

Related Questions