title: Creating dynamic buttons from an array in android
body: <p>I am creating a section where I retrieve a lot of names from array and display them in the form of buttons like how Soundcloud shows the trending genres. Can someone tell me how to achieve this or give me some hint? Right now, I did it with 2 for loops but it isnt working fine..<a href="https://i.stack.imgur.com/ai1gT.jpg" rel="nofollow">Check the image</a></p>
  
  <p>My code till now</p>
  
  <pre><code>for (int i = 0; i &lt; 3; i++) {
      LinearLayout row = new LinearLayout(this);
      row.setLayoutParams(new     LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
  
      for (int j = 0; j &lt; 4; j++) {
          TextView btnTag = new Button(this);
          btnTag.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
  
          Display display = getWindowManager().getDefaultDisplay();
          Point size = new Point();
          display.getSize(size);
          int width = size.x;
          int height = size.y;
          String someText = (web[(j + 1 + (i * 4))-1]) ;
          TextView myTextView = (TextView) findViewById(R.id.disid);
          float w = myTextView.getPaint().measureText(someText);
          tot = tot + w;
          btnTag.setBackgroundResource(R.drawable.buttonbg);
  
          Log.d("array", "CURRENT Activity ::"+ "final" + "  " + w+"  "+n+"  "+m);
          btnTag.setText(web[(j + 1 + (i * 4))-1]);
          btnTag.setId(j + 1 + (i * 4));
          row.addView(btnTag);
      }
      layout1.addView(row);
  }
  </code></pre>
  
  <p>But this generates only 4 buttons and 3 rows, I want it to be dynamic and decide the wrapping from the words it gets from the array. Please help me out!</p>