title: Dynamic GridLayout
body: <p>I made an error in another class, that's why it didn't work. The code below seems to be correct</p>
  
  <p>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:</p>
  
  <pre><code>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 &lt; 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++;
  </code></pre>
  
  <p>In the following image you can see what i get: Buttons 3 and 6 are missing. I'm afraid I am not using <code>GridLayout.spec</code> properly.</p>
  
  <p><img src="https://i.stack.imgur.com/f8USq.png" alt="enter image description here"></p>