title: android - adding button dynamically when each button width is different , automicatically flow to next row when width is full
body: <p>I need to create buttons dynamically to a linear layout. The width of each button will be wrap_content. Once the whole row is full, the button should be created in the next row. </p>
  
  <p>I have seen other answers in stackOverflow where this is done, but in those cases the no of buttons in a row is constant. In my case it is dynamic, because the width of each button is dynamic based on text width.</p>
  
  <p>It is similar to how tags are added to a question in this site.</p>
  
  <p>e.g. the answers in one of questions was:</p>
  
  <pre><code>LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags);
  layout.setOrientation(LinearLayout.VERTICAL);  //Can also be done in xml by android:orientation="vertical"
  
  
  for (int i = 0; i &lt; 3; i++) {
      LinearLayout row = new LinearLayout(this);
      row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  
      for (int j = 0; j &lt; 4; j++) {
          Button btnTag = new Button(this);
          btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
          btnTag.setText("Button " + (j + 1 + (i * 4));
          btnTag.setId(j + 1 + (i * 4));
          row.addView(btnTag);
      }
      layout.addView(row);
  }
  </code></pre>
  
  <p>But here the no of buttons in a row is constant. In my case , this will be different because the width of each button is different. </p>