Utilizing multiple buttons on Eclipse/Android - android

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)

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?

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

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

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>

Related

Accessing buttons in a GridLayout in Java?

I am making a hangman game, in the main Activity, there is a GridLayout containing the buttons for each letter.
I would like to set a Button array in java to include all of the buttons and access them (in order to use them in java methods, as my teacher instructed us). My question is, what would be the easiest way to identify the buttons in Java, and how can I add them to said Button array?
Thanks in advance. This is my first question here.
EDIT
The letters are not English letters and in this picture you can see the design of the GridLayout.
Notice that the language direction is RTL instead of LTR which means I used a reverse order of the columns.
So, in addition to my first question, is there any way to 'reverse' the numbers' order from LTR to RTL?
You don't need an array to identify the buttons. Use an array to identify just the buttons that was pressed.
If your button contains the letter as a text...:
<Button
android:id="#+id/alef_button"
...
android:text="א" />
<Button
android:id="#+id/bet_button"
...
android:text="ב" />
Then you can use the same method as a listener for all buttons:
public void onButtonClick(View view) {
Button pressedButton = (Button) view;
if("א".equals(pressedButton.getText())) {
putOnArray("alef");
} else if("ב".equals(pressedButton.getText())) {
putOnArray("bet");
} ...
}

Add a Button and Add String Resources

I am new to the world of android app creation. I am going through the tutorials. I am having trouble adding strings and buttons. Any help would be great. Thanks
to add a button lets say its "id" is "button1"
Declare it on main activity like below.
Button submit = (Button)findViewById(R.id.button1);
// The is of yout button is found in ur xml file.
Make sure you give your button an id in your XML file. If you want your button to have text it will tell you you need to give it a string name. You will find the strings xml file under res/values.
This is an example of how your button could look in your xml file
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button1" />
In the java code you need to do something like this to declare it.
Button b1 = (Button) findViewById(R.id.button1);

Change button android:background to different drawable

I am new to Android and Java but have managed to teach myself and find most answers to questions on stackoverflow without needing to ask questions. Until now....
Here goes, I have many colored buttons which, when clicked, change color to a range of different colors.
There are many buttons defined for example as:
<Button
android:id="#+id/button17"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#drawable/orange_button"
android:gravity="center"
android:onClick="onClick" />
Could someone please advise me how to change the android:background using code to change the above example to yellow, for example, when the button is clicked.
In the code below clickedButton is the ID of the button for which I need to change the background.
public void onClick(View v) {
int id=v.getId();
String clickedButton = getResources().getResourceEntryName(id);
Change button to Yellow here??
// Temporary code below to check which button was pressed
// and convert its number to an integer to eventually access an array
final TextView tvGameTimer = (TextView) findViewById(R.id.tvGameTimer);
int buttonNumber = Integer.parseInt(clickedButton.substring(6,8));
tvGameTimer.setText("" + buttonNumber);
}
I am using custom button styles to define the button colors:
res/drawable/yellow_button.xml
res/drawable/blue_button.xml
res/drawable/red_button.xml
res/drawable/orange_button.xml
res/drawable/green_button.xml
For now I just need to work out how to change the button from Orange to Yellow. I can then add the logic to change the colors as and when the app requires.
Many thanks for any help.
I am supposing that the onClick method you post is of the same Button whose background you are trying to change. Use this code.
v.setBackgroundResource(R.drawable.yellow_button);
If onClick is not the method of the same button then, use
findViewById(R.id.button17).setBackgroundResource(R.drawable.yellow_button);
Button btn = (Button) findViewById(R.id.button17);
btn.setBackground(this.getResources().getDrawable(R.drawable.yellow_button));
Try this.

Is it possible to change text on xml button?

If I have an xml button in a file called test.xml like this:
<Button
android:id="#+id/newgame"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New Game"
/>
Is it possible to change the "New game" text to something else? I have to do this many times.
The big problem is: I have to do it from inside a java file! Is that even possible?
Yes.
1) Get a reference to the Button
2) use the setText method to give text a new value
Button button = (Button)findViewById(R.id.newgame);
button.setText("Something Else");
definitly buddy,this type of question shoul not be ask for smart phones ,u can do it by update in xml as well as by java coding
ex....
Button buttontext = (Button)findViewById(R.id.button1);
button.setText("this is new text");
or in xml..
in xml it would be static for make it dynamic,set text in java class..
thanks

Android Eclipse development Clickable..text pops up

I have the following code:
<TextView
android:text="Color Yellow"
android:textColor="#000000"
android:gravity="center_horizontal"
android:background="#aaaa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:clickable="True"
/>
The android:clickable="True" was added because I thought it needed to be there (please do correct me if I'm wrong). However, the answer I'm seeking right now is how do I go by making another box (filled with text) pop-up upon clicking the "yellow box".
I would be grateful if someone could provide me with ideas and/or hints regarding how to actually create this scenario.
The android:clickable element does what you think and what it name tells you. It allows you to receive click events for that view (TextView here) to act on these.
To create a popup, you have to assign something to that TextView that tells you when it actually gets clicked. That is a OnClickListener. You can do that either in code or partially in code and XML. I'll just focus on the code example, but for the record, the XML one is also pretty easy. It involves setting the android:onClick="myOnClick" attribute to a certain function name that you like ("myOnClick" here) and creating a function like public void myOnClick(View v) in your activity.
What you have to do in code is
Referencing the TextView that you have in your layout
Assign the OnClickListener
Write an action that will be executed once the click gets registered
First point: To reference your TextView you have to use findViewById
TextView myTextView = (TextView) findViewById(R.id.mytextviewid);
Note that you have to assign a ID to your TextView to identify it. You can set that id via the android:id attribute in your XML layout (e.g. android:id="#+id/mytextviewid").
Second point: Once you have the reference, use TextView.setOnClickListener() to register one.
This usually looks like this:
myTextView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Add an action here
}
});
Third point: All you have to do now is displaying your dialog/message insite the onClick() function. There is more than one way to display that, you may use a Toast or an AlertDialog. Check out the links, there are some examples for that.

Categories

Resources