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

Android

Struggling 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
STAFF
Ben Jakuben
Treehouse Teacher

Looks pretty close! You just need to set inputTxt a little differently. You need to get the value from your EditText and store it there.

  1. 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).
  2. Instead of getString(R.id.editText1), you need to get the text from the EditText variable you just declared using the getText() method.
  3. The getText() method returns a valued of type Editable, which isn't quite the String data type you need. You can convert it by chaining toString(), as in this answer on StackOverflow: http://stackoverflow.com/a/4531500/475217

Like this?

public void onClick(View v) {
                //The button was clicked, so update the answer label with an answer
                TextView finalText = (TextView) findViewById(R.id.textView1);
                EditText inputTxt = (EditText) findViewById(R.id.editText1);
                //Initialize the two data types into variables

                String userText = inputTxt.getText().toString();
                //Takes the Editable data from the EditText and stores it as a string

                finalText.setText(userText);

For some reason my TextView thinks it's a boolean, and that everything is 'False' =s

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Strange! What happens if you write userText to the log or display it in a Toast message?