generate programmatically same layout with .xml file

VasilisK

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 );

                }
            }
Anastasios Zympidis

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Android programmatically include layout (i.e. without XML)

iOS SDK - Programmatically generate a PDF file

Could there be any performance difference between writing the same code (for the layout) in XML or java file?

Android Studio not identifying xml file as layout file

Android : Merging xml and programmatically generate layout

Generate and Design Rdlc file programmatically

How to set the XML attribute 'layout_constrainedWidth' of ConstraintLayout programmatically?

Python: Generate xml using xml & xslt file

Make layout by xml instead programmatically

Can I programmatically take a xml layout and reuse it?

Should repetitive views be drawn in xml layout or programmatically?

Can I access my XML layout programmatically through findViewById

Set runtime layout paramters in layout xml file

ScrollView and ListView in same xml layout

System did not generate an Id for my new xml layout and show me same errors

same xml file names in different layout folders issue(android)

Comparison between Android WebView programmatically and the XML-based Layout files

Generate a XML layout file dynamically

Complete layout programmatically by choosing from different XML files

confused with layout resource file with layout xml file?

Is there a way to programmatically make changes to your layout file?

Generate and save XML file

Short Key for Generate String From Layout XML

Layout XML File or Values XML File?

Generate a specific xml file

XML layout view not the same as emulator view

How can i programmatically add a view from XML to a Layout?

How to add cardview programmatically inside view but use layout declared in xml?

Programmatically generate image layout