Add a Button and Add String Resources - android

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);

Related

Questions related to android:onClick="selfDestruct" xml method

New to android and programming in general. This question might sound silly but I'd appreciate the answer. The question description and reasoning is at the beginning and the question is at the end of it all.
I want to apply a listener to a button in android. The way I understood from android.googlesource.com is that there is two way to do it:
applying an OnClickListener to the button in the activity.java
or
assign a method to my button in the xml layout using this
{#link android.R.attr#onClick android:onClick}
they gave the following xml layout example:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="selfDestruct"
android:text="#string/self_destruct" />
plus the the code in activity.java
public void selfDestruct(View view) {
// Kabloey
}
android.googlesource.com
Questions:
According to this: {#link android.R.attr#onClick android:onClick} android.R.attr in the example are the following:
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/self_destruct"
?
Does this:
android:text="#string/self_destruct"
android:onClick="selfDestruct"
mean that the button called self_destruct registered as a listener?
If I want to add more than one button listener in xml form how do I write it in the java document?
Thank you in advance
Really appreciate it.
Does this:
android:text="#string/self_destruct" android:onClick="selfDestruct"
mean that the button called self_destruct registered as a listener?
The button isn't "called" anything, it just has the text of the value for #string/self_destruct defined in the strings.xml file.
But, yes, the public void selfDestruct(View view) method is the method that will be called for the listener that is setup by the XML.
If I want to add more than one button listener in xml form how do I write it in the java document?
You can only set one click listener for a View.
Yes, layout_height, layout_width and text are attributes. You
can learn more about android attributes
here.
But keep in mind, that different views can use the same attributes
in different way.
It does not. The android:text attribute in this sample just refer to a string-resource called self_destruct to decide which text should be shown within the button. To distinguish views you can use android:id.
As already mentioned by other people, you are able to specify only one onClickListener using XML.

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.

Utilizing multiple buttons on Eclipse/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>

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

Change the text on a button in the program

The text on my buttons are set in resource files. E.g.
<Button android:id="#+id/up_button" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="#string/up_label" />
The above code defines a button with text #string/up_label. How can I change this text in my program during the application is running.
Thanks.
Button myButton = (Button) findViewById(R.id.up_button);
myButton.setText("my text");
maybe this will help.
Or, if you have the new text in resources (which I think you do), you do
Button myButton = (Button) findViewById(R.id.up_button);
myButton.setText(R.string.down_label);
where down_label is the id of your string.
Button button = (Button) findViewById(R.id.up_button);
For example:
button.setText("This is how to change text at runtime");

Categories

Resources