Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialkeninakennedy
969 PointsAndroid Strings
How do I set the text of the Button using the 'label' variable
keninakennedy
969 PointsThanks Kasim, I'm getting compiler error
1 Answer
Daniel Hartin
18,106 PointsFirstly you need to set up a Button variable and assign it the id of the Button view you have created in your xml file. It should go something like this.
Button myButton = (Button) findViewById(R.id.button1);
Then you can use the method myButton.setText("whatever text you want");
if you already have a string variable set up called label you can simply add that into the brackets instead like
myButton.setText(label);
Hope this helps
Daniel
keninakennedy
969 PointsHi Daniel,
Thanks for the reply. I've tried using the string variable but I'm getting a compiler error.
Daniel Hartin
18,106 PointsCan you post your source code? it might be easier if I can take a look at the code you already have?
keninakennedy
969 PointsstringTest.java
String[] phoneNumberLabels = { "-home", "-work", "-mobile" };
Button saveButton = (Button) findViewById(R.id.saveButton); String label=getString(R.string.saveLabel); saveButton.setText("SAVE");
String.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="saveLabel">Save My Changes</string> <string name="label">Save</string> </resources>
Daniel Hartin
18,106 PointsOkay are you trying to set the text of the button equal to a value in your phoneNumberLabels array or a string resources in your strings.xml file?
in order to set the text equal to a value in your string resource folder use the following
saveButton.setText(R.string.save_button_text); you will also need something like this in your string resources file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="save_button_text">Save Button</string>
</resources>
if you wish to set the text of your button to a value in an array use the following
saveButton.setText(phoneNumberLabels[1]) (or whatever position of the array you wish).
You can delete the line String label=getString(R.string.saveLabel);
I hope I understand what you are trying to do. As the java code appears correct I assume the problem must lie with the xml file somewhere.
If this doesn't help, on what line are you getting an error or does eclipse allow you to run the android app and the app stops while running?
Kasim Khan
4,925 PointsKasim Khan
4,925 Points'''Button.LabelName.Text = @" ";
Try that?