title: Android : How to create this layout dynamically?
body: <p>I want to create a layout dynamically in android which consist some buttons. But I am unable to find any way to dynamically create this layout. </p>
  
  <p>Here is my layout : </p>
  
  <p><img src="https://i.stack.imgur.com/6corU.png" alt="Dynamic Layout"></p>
  
  <p>I want to put buttons into next line after they completely filled in the width of device. Total number of buttons can be varied. 
  Is there any way to do this dynamically?</p>
  
  <p><strong>UPDATE :</strong>
  As per your suggestions I'm using this code :</p>
  
  <pre><code>private void addQuestionPallete() {
          int tempBtn = 30;
          LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 0.1f);
          LayoutParams param2 = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f);
  
          linearContent.setWeightSum(1f);
          linearContent.setOrientation(LinearLayout.VERTICAL);
          btnQuestionPalete = new Button[10];
  
          for (int j = 0; j &lt; tempBtn / 10; j++) {
              linearQuestionPalettesRow = new LinearLayout(mContext);
              linearQuestionPalettesRow.setOrientation(LinearLayout.HORIZONTAL);
              linearQuestionPalettesRow.setLayoutParams(param2);
              for (int i = 0; i &lt; 10; i++) {
                  btnQuestionPalete[i] = new Button(mContext);
                  btnQuestionPalete[i].setLayoutParams(param);
                  btnQuestionPalete[i].setTextColor(Color.BLACK);
                  btnQuestionPalete[i].setText("5");
                  btnQuestionPalete[i].setId(fChexkBoxID++);
                  linearQuestionPalettesRow.addView(btnQuestionPalete[i]);
              }
          }
          linearContent.addView(linearQuestionPalettesRow);
      }
  </code></pre>
  
  <p>but its only showing single row of buttons. Where I'm doing wrong?</p>
  
  <p><strong>UPDATE :</strong></p>
  
  <p>OK I have solved my problem. I was adding layout out of for loop. thus the layout is adding only one time.</p>