androidandroid-layoutandroid-gridlayoutlayoutparams

generate programmatically same layout with .xml file


i have a gridLayout filled with some imageviews in my xml file and i want to create a copy of the gridlayout programmatically

.xml file

<GridLayout
        android:id="@+id/gridLayoutBucket"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:columnCount="3"
        android:padding="24dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="24dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textTimerBucket"
        app:layout_constraintBottom_toTopOf="@id/gridLayoutBucketBucket"
        android:rowCount="2">

        <ImageView
            android:id="@+id/imageView1Bucket"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:contentDescription="@string/imageDesc"
            android:layout_margin="4dp" />

        <ImageView
            android:id="@+id/imageView2Bucket"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:contentDescription="@string/imageDesc"
            android:layout_margin="4dp" />

        <ImageView
            android:id="@+id/imageView3Bucket"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:contentDescription="@string/imageDesc"
            android:layout_margin="4dp" />

        <ImageView
            android:id="@+id/imageView4Bucket"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:contentDescription="@string/imageDesc"
            android:layout_margin="4dp" />

        <ImageView
            android:id="@+id/imageView5Bucket"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:contentDescription="@string/imageDesc"
            android:layout_margin="4dp" />

        <ImageView
            android:id="@+id/imageView6Bucket"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:contentDescription="@string/imageDesc"
            android:layout_margin="4dp" />


    </GridLayout>

my try to recreate the gridLayout ,its really close but the second lane crop some space of my imageview I create a gridLayout and then set the appropriate rows and cols, In addition i create a rowSpec and a colSpec then i fill my gridlayout with imageviews givin them some layoutparams like width height , the rowSpec annd the ColSpec

        GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayoutBucket);
        gridLayout.removeAllViews();
        gridLayout.setColumnCount(3);
        gridLayout.setRowCount(2);


        int idsetter = 0;

            for(int i=0; i<gridLayout.getRowCount(); i++)
            {
                GridLayout.Spec rowSpec = GridLayout.spec(i, 1,GridLayout.FILL,1);

                for(int j=0;j<gridLayout.getColumnCount();j++)
                {
                    GridLayout.Spec colSpec = GridLayout.spec(j,1,GridLayout.FILL,1);

                    ImageView imageView = new ImageView(this);
                    imageView.setId(idsetter);
                    GridLayout.LayoutParams myGLP = new GridLayout.LayoutParams();
//                    myGLP.rowSpec = rowSpec;
//                    myGLP.columnSpec = colSpec;
                    GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();
                    layoutParams.width = 0;
                    layoutParams.height = 0;
                    layoutParams.rowSpec = rowSpec;
                    layoutParams.columnSpec = colSpec;
                    imageView.setLayoutParams(layoutParams);
                    gridLayout.addView(imageView, myGLP );

                }
            }

Solution

  • Your problem lays with the XML format and especially the GridLayout padding that you have on the Grid. By assigning android:layout_width="0dp" android:layout_height="0dp", i am assuming that you have another View on the same XML with these Width and Height. By doing that you are telling these 2 Views to share the screen that is left. Now that is ok but by also putting padding on the GridLayout you are making the View "Stretch" 24dp to every direction thus making the other View overlap with GridLayout resulting in this cropping that you are describing in the second row. Try removing the padding on the GridLayout and i think you will be set.