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:
How to go to the next line programmatically?
This will help
This is like 1 answer, but without the need to make a XML file.
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.
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
I had the problem, that the number of views to be placed on the UI was set during runtime. So I decided that I want only four views in one row and adapted the code above accordingly. Also, I always filled up the last row with invisible edit texts so that the views in the last row also had the same width as the views in the row above: