I am new to programming but had some spare time and just got a new android tablet so thought its time to learn. I play a board game that has MANY decks that you draw from throughout the course of the game and decided it would be nice to simply have an application showing the 21 decks and you click on one and it randomly displays a card from one of those decks. You read the card, act on it, click on the card and it disappears.
i thus have a layout with all 21 decks (7×3) each an individual button. Thus i have 21 buttons on the 1 screen. According to the tutorial i have been following i need to declare the buttons on the .java file button1 = (Button) findViewById(android.R.id.button1). but it only has the option to declare 3 buttons after which i get the little red x of doom.
How do i go about declaring all 21 buttons? Or do i not need to declare these buttons?
Any help would be great! (may also need help finding a way to randomize the “draw” feature so don’t be surprised to see me again)
If you laid out each button in XML (main.xml or something similar), then yes, if you wish to have them do anything, you have to declare the buttons as you said.
By typing it this way, I assume you didn’t declare the buttons higher up in your code, as class-wide fields. Also, have you run the method setContentView(R.layout.main);?
So let’s just be clear: unless you type Button b1; Button b2; Button b3 just below your class line (public class YourClassName() {, each time you try to instantiate a button, you have to say Button b1 = (Button) findViewById(R.id.button1);. If you did make class-wide fields (right below the class line) then you can have code like you showed in your original question, where it’s just button1 = (Button) findViewById(R.id.button1). Does this distinction make sense?