title: Android - How to add buttons programmatically depending on screen size and button below each other dynamically?
body: <p>I'm experiencing a little problem to make my Android app.
  I want to have a responsive and dynamical application.</p>
  
  <p>When the user clicks on a button, i want to add button dynamically, here is my code:</p>
  
  <pre><code> @Override
  public boolean onOptionsItemSelected(MenuItem item) {
  
  
      switch (item.getItemId()) {
          case R.id.action_ajouter:
  
              Button b = new Button(this);
  
  
                 //numButton counts the number of buttons created by
                //clicking on a button placed in the action bar
  
                  if (numButton % 2 != 0) {
                      //Align to the left
  
                      RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                      params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                      b.setLayoutParams(params);
                      b.setText("Bouton n°" + numButton);
                      mainRelativeLayout.addView(b);
                      numButton++;
  
                  } else {
                      //Align to the right
                      RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                      params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                      b.setLayoutParams(params);
                      b.setText("Bouton n°" + numButton);
                      mainRelativeLayout.addView(b);
                      numButton++;
                  }
  
              break;
          default:
              break;
      }
      return true;
  
  
      }
  </code></pre>
  
  <p><strong>The problem is:</strong> <strong>I need two buttons to be added side by side and the other ones blow those ones (also side by side). The problem is they are superposing them and not being added below the others.</strong></p>