12

I made an error in another class, that's why it didn't work. The code below seems to be correct

I'm trying to create a dynamic GridLayout. Inside another class, not this one, I have a method that designs rows and cols of my gridlayout. In the class below, i add some buttons to my GridLayout:

int buttons= 6;//the number of bottons i have to put in GridLayout
int buttonsForEveryRow = 3; // buttons i can put inside every single row
int buttonsForEveryRowAlreadyAddedInTheRow =0; // count the buttons added in a single rows
int columnIndex=0; //cols index to which i add the button
int rowIndex=0; //row index to which i add the button

for(int i=0; i < buttons;i++){          
    /*if numeroBottoniPerRigaInseriti equals numeroBottoniPerRiga i have to put the other buttons in a new row*/
    if(buttonsForEveryRowAlreadyAddedInTheRow ==buttonsForEveryRow ){
        rowIndex++; //here i increase the row index
        buttonsForEveryRowAlreadyAddedInTheRow  =0;  
        columnIndex=0; 
    }   

    Spec row = GridLayout.spec(rowIndex, 1); 
    Spec colspan = GridLayout.spec(columnIndex, 1);
    GridLayout.LayoutParams gridLayoutParam = new GridLayout.LayoutParams(row, colspan);
    gridLayout.addView(button_to_add,gridLayoutParam);

    buttonsForEveryRowAlreadyAddedInTheRow ++;
    columnIndex++;

In the following image you can see what i get: Buttons 3 and 6 are missing. I'm afraid I am not using GridLayout.spec properly.

enter image description here

Bowdzone
  • 3,827
  • 11
  • 39
  • 52
MDP
  • 4,177
  • 21
  • 63
  • 119
  • 1
    What do you mean? You mean i have to set the cols and rows numbers inside the layout.xml file? – MDP Feb 06 '13 at 13:41
  • no set it through code – Abhishek Nandi Feb 06 '13 at 13:47
  • aaaa, i did it, inside another class. I used `gridLayout.setColumnCount` and `gridLayout.setRowCount`. After setting them, i checked them with `getColumnCount` and `getRowCount` and i saw that rows and cols are correct. probably the problem is that i don't add my buttons to the GridLayout properly – MDP Feb 06 '13 at 13:51
  • OH made an error in another class, that's why it didn't work. Anyway thank you – MDP Feb 06 '13 at 14:12

1 Answers1

16

Using below code you can add image views to grid layout dynamically with column span and row span.

    gridLayout = (GridLayout) findViewById(R.id.gridview);

    gridLayout.removeAllViews();

    int total = 10;
    int column = 3;
    int row = total / column;
    gridLayout.setColumnCount(column);
    gridLayout.setRowCount(row + 1);
    for (int i = 0, c = 0, r = 0; i < total; i++, c++) {
        if (c == column) {
            c = 0;
            r++;
        }
        ImageView oImageView = new ImageView(this);
        oImageView.setImageResource(R.drawable.ic_launcher);            

        oImageView.setLayoutParams(new LayoutParams(100, 100));

        Spec rowSpan = GridLayout.spec(GridLayout.UNDEFINED, 1);
        Spec colspan = GridLayout.spec(GridLayout.UNDEFINED, 1);
        if (r == 0 && c == 0) {
            Log.e("", "spec");
            colspan = GridLayout.spec(GridLayout.UNDEFINED, 2);
            rowSpan = GridLayout.spec(GridLayout.UNDEFINED, 2);
        }
        GridLayout.LayoutParams gridParam = new GridLayout.LayoutParams(
                rowSpan, colspan);
        gridLayout.addView(oImageView, gridParam);


    }
Arunkumar MG
  • 466
  • 1
  • 5
  • 14
  • 15
    Hi, welcome to StackOverflow. Please don't just post code as an answer. Explain your thoughts so we can better understand what you have done. Thanks. – Cthulhu Jul 23 '15 at 08:42
  • 15
    @Strive55 I do not agree. He takes time to post code and if you want to, you can figure out yourself how it works. It not that big of a snippet and pretty easy to understand – Boy Aug 11 '15 at 14:03
  • 1
    @boy He is simply following the requirements of posting answers for this site. – Thom Sep 06 '16 at 18:54
  • If there's a total of 12 items then the row count would be 5, but we only need 4. – 6rchid Feb 26 '19 at 02:53