title: aligning dynamic buttons in gridlayout
body: <blockquote>
    <p>I am trying to align dynamic buttons in <code>Grid layout</code>.</p>
  </blockquote>
  
  <p><strong>Problem:</strong> </p>
  
  <blockquote>
    <p>However, if text is more than one line the formation of buttons in row
    is not as needed.</p>
    
    <p>String item[] =  new String[]{"item1", "item2", "item3 yes",
    "item4","item5"};</p>
    
    <p>Button[] myButton = new Button[item.length];</p>
  </blockquote>
  
  <pre><code>GridLayout scrViewButLay = (GridLayout) findViewById(R.id.grid);
      scrViewButLay.setColumnCount(4);
      scrViewButLay.setRowCount(10);
  </code></pre>
  
  <p>// GridLayout.LayoutParams leftMarginParams = new GridLayout.LayoutParams(
         //     );</p>
  
  <pre><code> for (int index = 0; index &lt; item.length ; index++) {
  
  
          myButton[index] = new Button(this); //initialize the button here
          myButton[index].setText(item[index]);
          myButton[index].setHeight(100);
          myButton[index].setWidth(100);
          myButton[index].setTag(index);
  
          scrViewButLay.addView(myButton[index]);
  
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/x2Iah.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/x2Iah.png" alt="output image"></a>
  `</p>