I’m trying to add 24 buttons with a for-loop. I’m trying to place four equally sized buttons per row and hoping to see the next group of four to appear below them.
How can I achieve this?
My not-working effort so far looks like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView sv = new ScrollView(this);
LinearLayout layout = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout.setWeightSum(12.0f);
params.weight = 3.0f;
layout.setOrientation(LinearLayout.HORIZONTAL);
sv.addView(layout);
for (int i = 0; i < 24; i++) {
Button btn = new Button(this);
btn.setText("Button");
btn.setLayoutParams(params);
layout.addView(btn);
}
this.setContentView(sv);
}
Any help appreciated.
This is how you add the
Buttonslike you want: