0
votes

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:

enter image description here

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.

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.

To simple add four buttons, I could do like this:

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 < 3; i++) {
        LinearLayout row = new LinearLayout(this);
        row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        for (int j = 0; j < 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);
}
}

And to programmatically set margins for each button(view), I could do like this:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 0, 0);
button.setLayoutParams(params); 

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.

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! :)

1
Please don't prefix your question's titles with tag names like *Android -*(the tags at the bottom are enough to mention the question's target). As you want the buttons in form of a list shouldn't you also be setting the orientation to vertical for the LinearLayouts that you build in the for loop?user
Thanks for your advice. I would not use Android as the title next time and in terms of the example, I just use it as a demo of how buttons could be added into linearlayout. For my case, I actually dont need to build 3 views each with 4 buttons, I only need to build one view with 4 buttons in it. Also I want to be able to set the margins for each button programmatically. :)jinnancun

1 Answers

3
votes
        linear = (LinearLayout) rootView.findViewById(R.id.linear);
    .
    .
    .   

     RelativeLayout.LayoutParams layoutParam = new RelativeLayout.LayoutParams(
                            RelativeLayout.LayoutParams.MATCH_PARENT,
                            RelativeLayout.LayoutParams.WRAP_CONTENT);
                    LinearLayout layout = new LinearLayout(getActivity());
                    layout.setLayoutParams(layoutParam);
                    layout.setOrientation(LinearLayout.VERTICAL);

                    // Below will add three linear layout with 4 buttons in each
                    for (int i = 0; i < 3; i++) {
                        LinearLayout row = new LinearLayout(getActivity());
                        row.setLayoutParams(new LinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.MATCH_PARENT,
                                LinearLayout.LayoutParams.WRAP_CONTENT));
//Here is important
                        row.setOrientation(LinearLayout.VERTICAL);

                        for (int j = 0; j < 4; j++) {
                            Button btnTag = new Button(getActivity());
                            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);
                    }
                    linear.addView(layout);
                    // You can set also
                    // setContentView(layout)