1

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 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.