Android Build an Interactive Story App Intents and Multiple Activities Getting Text from an EditText

Ben Rogers
Ben Rogers
2,149 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.

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

2 Answers

Rebecca Rich
PRO
Rebecca Rich
Pro Student 8,484 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
2,149 Points

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