Changin item's weight in the code (Android Studio)

53 views Asked by At

I have a GridLayout where I want to put Imageviews like this: This is what I want Where I set the column's and row's weight to one.

But I'm putting these Imageviews into the grid via code like this:

 private void addQRCodeToGridLayout(Bitmap qrCodeImage) {
        ImageView imageView = new ImageView(this);
        imageView.setImageBitmap(qrCodeImage);

        imageView.setBackgroundResource(R.drawable.roundstyle);
        imageView.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#D3D3D3")));
        int paddingInDp = 10;
        float scale = getResources().getDisplayMetrics().density;
        int paddingInPx = (int) (paddingInDp * scale + 0.5f);
        imageView.setPadding(paddingInPx, paddingInPx, paddingInPx, paddingInPx);

        GridLayout.LayoutParams params = new GridLayout.LayoutParams();
        params.rowSpec = GridLayout.spec(GridLayout.UNDEFINED, 1f);
        params.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, 1f);
        params.width = GridLayout.LayoutParams.WRAP_CONTENT;
        params.height = GridLayout.LayoutParams.WRAP_CONTENT;
        params.setMargins(10, 10, 10, 10);


        imageView.setLayoutParams(params);

        gridLayout.addView(imageView);
    }

But in the end it looks like this: This is on my phone So it seems like it does most of the things except set the weight to one.

I read multiple pages, but did't find anything. I can't understand why it isn't changing like in the "preview".

0

There are 0 answers