I am working on a mini-game that includes 40 different buttons. I guess I can make them manually on the XML code and then group them into an array, but can I make buttons using the code in the first place?
1 Answer
Yes, you can do it from code as well:
Button button = new Button(getContext());
button.setText("Button_text");
button.setId(xyz);
// ... add onClickListener etc.
// Add it to parent view, e.g. LinearLayout
parentLayout.addView(button);
If you have to add 40 different buttons, wrap it to a for loop for example.