Search code examples
androidbuttonfor-loopalphabet

ANDROID: How can I set up buttons in a for loop?


Does anybody know how to create buttons for the alphabet (for a hangman-application) in a for loop?
I'm not sure what needs to be done in the java class and what needs to be done in the xml file.


Solution

  • You do not need to do anything in the XML file. This can all be done in a class.

    for(int i=0; i < x; i++) // where x is the size of the list containing your alphabet.
    {
       Button button = new Button(this);
       button.setId(i);
       yourView.add(button);
    }