1

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 (7x3) 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)

Milad Nouri
  • 1,587
  • 1
  • 21
  • 32
bribrem
  • 37
  • 1
  • 5

4 Answers4

0

You do need to define each button. Use the follow:

Button button1 = (Button)findViewById(R.id.idofbutton1);
Button button2 = (Button)findViewById(R.id.idofbutton2);
Button button3 = (Button)findViewById(R.id.idofbutton3);
Button button4 = (Button)findViewById(R.id.idofbutton4);

so forth so on

testingtester
  • 528
  • 4
  • 11
0

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.

Button button1 = (Button) findViewById(R.id.button1);

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?

Davek804
  • 2,804
  • 4
  • 26
  • 55
  • the setContentView(R,layout.main); is the line directly above the line i am typing on (not sure what it does; was part of the code set by eclipse) – bribrem Apr 09 '12 at 00:43
  • Did you layout your buttons in an XML file? res/layout/main.xml for example? If so, setContentView() is what tells your .java file what the screen should look like. It sets the View to your layout file, which I suspect is main.xml(or wherever your buttons were laid out). So anyways, are you dealing with an error? If so, what is it? – Davek804 Apr 09 '12 at 00:45
  • I have the code Button button1 = (Button) findViewById(R.id.button1); and the first button1 is underlined in yellow and says (when curser held over) "the value of the local variable button1 is not used. Will that go away once i attatch a command to the button? – bribrem Apr 09 '12 at 00:53
  • Yes it will. If you, for example, said button1.setOnClickListener(listener); (or any other method) it would be used, and that message would disappear. – Davek804 Apr 09 '12 at 00:55
  • k; got a lot of coding to do now - will update on if it works or not! thanks for the help! – bribrem Apr 09 '12 at 00:56
  • Np. Good luck. Don't forget, if you declare all of the buttons fully inside onCreate (Button b1 = (Button)...) then you will not be able to access them from another method. If you declare the buttons (but not instantiate) as fields (right below the class line), you will be able to access them anywhere in the class. If you go down this route, inside onCreate, each instantiation only needs to read: button1 = (Button) findViewById(R.id.button1). Good luck! – Davek804 Apr 09 '12 at 00:59
0

Also, if you wanted to do this inside a loop, you could do that too. Might make things a bit easier. Here's a link: https://stackoverflow.com/a/8687807/1231943

Community
  • 1
  • 1
Davek804
  • 2,804
  • 4
  • 26
  • 55
0

Anytime you declare a Button (which is an object):

Button button1 = (Button)findViewById(R.id.idofbutton1);
Button button2 = (Button)findViewById(R.id.idofbutton2);

Make sure you add the "id" to the XML layout:

 <Button
   android:id="@+id/idofbutton1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Button1"
   android:textSize="20px" >
</Button>

<Button
   android:id="@+id/idofbutton2"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Button2"
   android:textSize="20px" >
</Button>
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185