title: Adding multiple buttons on the fly
body: <p>I want to add multiple buttons dynamically through the code on button click, I searched many previous posts which shows to add single button, but I need multiple ones.</p>
  
  <p>Attached is the sample code.</p>
  
  <pre><code>   public class MainActivity extends Activity {
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Button b1 = (Button)findViewById(R.id.button1);
          b1.setOnClickListener(new View.OnClickListener() {      
              @Override
              public void onClick(View v) {               
                  AddAll();                   
              }
          });
      }
      public void AddAll() {
          final RelativeLayout rl = (RelativeLayout)findViewById(R.id.rel);
          final Button btn = new Button(this);
          for(int i=0;i&lt;4;i++)
          {
              rl.addView(btn); 
              btn.setText("hello");
              btn.setWidth(320);
              btn.setHeight(40);
          }
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.activity_main, menu);
          return true;
      }
  </code></pre>
  
  <p>Please help regarding the same. However adding single button is working fine, but I need to add many buttons one below the other.</p>