How do I programmatically add buttons into layout one by one in several lines?

How to create a list of buttons one by one in several lines? I made this:

LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags);
    for (int i = 1; i < 10; i++) {
        Button btnTag = new Button(this);
        btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        btnTag.setText("Button " + i);
        btnTag.setId(i);
        layout.addView(btnTag);
        ((Button) findViewById(i)).setOnClickListener(this);
    }

and got only one line:

I got this
How to go to the next line programmatically?


Solution 1:

The issue is that your buttons are not going to automatically wrap to the next part of the screen. You have to specifically tell Android how you want your Views to be positioned. You do this using ViewGroups such as LinearLayout or RelativeLayout.

LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags);
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 LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        for (int j = 0; j < 4; j++) {
            Button btnTag = new Button(this);
            btnTag.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            btnTag.setText("Button " + (j + 1 + (i * 4)));
            btnTag.setId(j + 1 + (i * 4));
            row.addView(btnTag);
        }

        layout.addView(row);
    }

I'm assuming that R.id.linear_layout_tags is the parent LinearLayout of your XML for this activity.

Basically what you're doing here is you're creating a LinearLayout that will be a row to hold your four buttons. Then the buttons are added and are each assigned a number incrementally as their id. Once all of the buttons are added, the row is added to your activity's layout. Then it repeats. This is just some pseudo code but it will probably work.

Oh and next time be sure to spend more time on your question...

https://stackoverflow.com/questions/how-to-ask

Solution 2:

This is like 1 answer, but without the need to make a XML file.

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);
    }
} 

Solution 3:

This will help

private void addingParticipantinFrame() {
        TableRow row;
        final int screenWidth = dpToPx(getResources().getConfiguration().screenWidthDp);
        add_button_particiapants.setVisibility(View.VISIBLE);
        if (arrrItemSelcted != null && arrrItemSelcted.size() > 0) {

            arrrItemSelcted.remove(0);
            int i = 0;
            int j = 0;
            int width = (screenWidth - (arrrItemSelcted.size())*4)/arrrItemSelcted.size();

            while (i<arrrItemSelcted.size()) {
                j = i + 4;
                row = new TableRow(mParentActivity);
                TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
                row.setLayoutParams(lp);
                row.setWeightSum(4);
                while ((i<j)&&(i<arrrItemSelcted.size())) {
                    Button iBtn = new Button(mParentActivity);
                    iBtn.setGravity(Gravity.CENTER_HORIZONTAL);
                    iBtn.setMinimumWidth(100);//I set 100px for minimunWidth.
                    iBtn.setWidth(width);
                    iBtn.setText(Integer.toString(i + 1));
                    iBtn.setId(i + 1);
                    row.addView(iBtn, 4 + i - j);
                    i++;
                }
                add_button_particiapants.addView(row);
            }



            }
        }