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 Build an Interactive Story App Intents and Multiple Activities Getting Text from an EditText

Ben Rogers
Ben Rogers
6,767 Points

I can't figure this question out. It looks like very different code than the one in the tutorial

I've been stuck on this for a while. I'm not entirely sure what is being asked of me.

EditText petNameField = (EditText)findViewById(R.id.petNameField);
String petName;

2 Answers

Rebecca Rich
Rebecca Rich
Courses Plus Student 8,592 Points

Somewhere in an .xml file there is an EditText component with the ID petNameField. The first line of this sample code creates a reference to the EditText component, which is an editable text extension of TextView. Here is more information about EditText: https://developer.android.com/reference/android/widget/EditText.

When you have petNameField as a reference to the EditText in the view, you simple have a reference to the view component, not the actual text entered into that view by a user. The goal of the challenge is to get the text entered into the EditText by a user and store it as a String, petname. To do so, we need to call a method on petNameField. If you look at the list of public methods on the link above, you will find the getText() - a method described to be able to "Return the text that TextView is displaying." This looks like a great fit, but only returns an Editable, so we will also have to call getString(). Altogether, we will need:

String petName = petNameField.getText().toString();

to get the pet name from the pet name field.

Does this help?

Ben Rogers
Ben Rogers
6,767 Points

Great! It all makes sense now. Thanks a bunch!