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 trial
Cassius Brophy
2,128 PointsStruggling with extra credit task
I'm stuck on the Java extra credit on Getting Started With Android. For your convenience, the task: "Add a label (TextView) to a layout with an input field (EditText) and button below it. See if you can get the EditText and Button on the same line. Then add an onClickListener to the button that takes the text entered in the EditText and sets it as the text for the TextView."
I've been trying things for a while now, and the closest I've got is the TextView returning a boolean instead of the EditText. Relevant code:
Button setTextButton = (Button) findViewById(R.id.button1);
setTextButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//The button was clicked, so update the answer label with an answer
TextView givenText = (TextView) findViewById(R.id.textView1);
String inputTxt = getString(R.id.editText1);
givenText.setText(inputTxt);
}
Halp pls?
1 Answer
Ben Jakuben
Treehouse TeacherLooks pretty close! You just need to set inputTxt a little differently. You need to get the value from your EditText and store it there.
- Declare an EditText variable in the same way you have your TextView declared. You'll need the ID (looks like it might be R.id.editText1).
- Instead of
getString(R.id.editText1), you need to get the text from the EditText variable you just declared using the getText() method. - The
getText()method returns a valued of typeEditable, which isn't quite theStringdata type you need. You can convert it by chainingtoString(), as in this answer on StackOverflow: http://stackoverflow.com/a/4531500/475217
Cassius Brophy
2,128 PointsCassius Brophy
2,128 PointsLike this?
For some reason my TextView thinks it's a boolean, and that everything is 'False' =s
Ben Jakuben
Treehouse TeacherBen Jakuben
Treehouse TeacherStrange! What happens if you write
userTextto the log or display it in a Toast message?