title: Android - How to add several buttons with different layout_margins in a LinearLayout programmatically?
body: <p>I am trying to building a linear layout programmatically, in which, there are 4 buttons in all, each of the button simply positioned below the previous button. Just like the picture shows below:</p>
  
  <p><img src="https://i.stack.imgur.com/Qi1vN.png" alt="enter image description here"></p>
  
  <p>And as you can see from the picture above, each button has exactly the same size but they have different layout_margins, the first button has a larger value in layout_marginTop while the other 3 buttons has the same value in layout_marginTop.</p>
  
  <p>Basically to build a layout like this by xml is very simple but now I have really come across difficulties in building all this only through java code. I have gone through many posts and now I can easily add 4 buttons in proper sizes but I just could not find a proper way to programmatically set the layout_margin for each of the button. </p>
  
  <p>To simple add four buttons, I could do like this:</p>
  
  <pre><code>public class mainActivity extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      LinearLayout layout = new LinearLayout(this);
      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);
      }
      setContentView(layout);
      //setContentView(R.layout.main);
  }
  }
  </code></pre>
  
  <p>And to programmatically set margins for each button(view), I could do like this:</p>
  
  <pre><code>LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
              LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  params.setMargins(0, 2, 0, 0);
  button.setLayoutParams(params); 
  </code></pre>
  
  <p>But here comes the problem: as long as I setLayoutParams for the 4 buttons, and then add 4 buttons into Linearlayout by "addView(button)", only the first button could be displayed in the proper size with the proper margins. All the other 3 buttons just disappeared. And With many tests I just found it seems that only one layoutParams could be allowed in a linearlayout object. As a result, as long as I set different layout params for different buttons, only the first button could be displayed. But since here the margin params for my 4 buttons are definitely different thus I think I have to use different layoutparams for different buttons.</p>
  
  <p>So pls anyone tell me how could I programmatically set the margin for my each of my buttons and make them display properlly? This has already sucked my life for two days, pls help! :)</p>