Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Getting input from an EditText is as simple as calling a method on it. But we need to make sure we do it in the right place, and that we know what to do with what the method returns.
Documentation
Related Links
- Controlling Access to Members of a Class - public vs. private, etc.
- How are Anonymous (inner) classes used in Java?
GitHub Repo
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
[SOUND]
0:00
[MUSIC]
0:01
[SOUND] We just proved that we can
enter text in the main field, great.
0:03
But how do we access that text and
use it in our code?
0:08
>> Let's go back into our
MainActivity class file.
0:12
Now notice over here I have a new class
called InteractiveStoryApplication.
0:15
If you didn't watch the optional video in
the last section, don't worry about this.
0:19
You do not need this.
0:22
It's only there if you went
through that optional video.
0:23
So here in our class let's
add a new member variable.
0:26
private EditText, and
we'll call this nameField.
0:30
Could call it name EditText but I kind of
get tired typing EditText over and over.
0:34
Field is a bit easier and
means the same thing.
0:38
Remember that private here means that this
field can only be used inside this class,
0:41
which is what we want.
0:45
We will generally make member
variables private like this,
0:45
though sometimes we want them
to be public or protected.
0:49
Making things public is okay, but it does
open up our code to more opportunities for
0:51
misuse or unintended consequences.
0:55
Now why is onCreate protected down here?
0:59
Protected is kind of a cross
between public and private.
1:01
It makes things available to other
classes in the same package or
1:04
subclasses of this class.
1:07
This is useful in certain situations, but
1:09
we'll talk about that when we
come across it in the future.
1:11
You may already know all about
inheritance from Java, but
1:13
we haven't really talked
about it much yet.
1:15
And we're going to deal
with it more in the future.
1:17
Briefly though, inheritance is an object
oriented concept where a class
1:19
can inherit things from a parent
class by extending it as a subclass.
1:23
So we see this with MainActivity.
1:28
We are extending the AppCompatActivity
class, which means that our main activity
1:30
is a subclass, or
child of the AppCompatActivity class.
1:34
That means that MainActivity is
the same as a regular AppCompatActivity
1:38
plus all the extra stuff
that we add to it in here.
1:42
It's worth pointing out that
AppCompatActivity itself extends
1:46
FragmentActivity, which extends Activity.
1:49
Every kind of special activity in
Android is somehow a descendant of this
1:52
base activity class.
1:56
And if we keep walking
up this parent-chain,
1:57
we end up at the mother of all objects,
java.lang.Object.
1:59
There's one more thing back in here.
2:04
We haven't talked about this Override
annotation that we see here.
2:05
Java annotations like this are hints
to the compiler that help us
2:08
while developing.
2:12
They aren't actually required for
the code to work.
2:13
This hint states that this method
is overriding the onCreate method
2:15
of the parent class.
2:19
We see this same thing a lot in Android.
2:21
Anyhow, we can set our new member
variable here in the onCreate method.
2:23
Remember to do it after setContentView,
2:27
because that's the method that
attaches our layout to the activity.
2:29
And without that, we won't have anything
for the edit text to be set to.
2:32
So let's start typing nameField = and
then we'll call findView by ID.
2:36
But remember that we need to
cast it to the appropriate view.
2:41
Because findViewById
returns a generic view.
2:43
And EditText is a subclass
of the View class.
2:46
For the ID we want R.id.
2:50
and we called it nameEditText.
2:51
Now where should we try to access
the value that the user typed
2:54
inside this EditText.
2:58
It won't do us much good to do
anything here in the onCreate method.
2:59
Because this only runs when
the activity is first created and
3:02
the EditText will be blank.
3:05
This method sets up the activity and then
we're ready to accept input from the user,
3:06
that's why we put a button on the screen.
3:11
The user should enter their name
before tapping on the button.
3:12
Remember what code we used
when the button is tapped?
3:15
That's right, an onClickListener.
3:17
So let's add a new member variable
up here, call this private Button,
3:19
startButton, And
3:24
then I'm gonna give myself a little
bit more room on this screen.
3:27
I'm going to collapse
the import statements, and
3:29
add a few lines at the bottom.
3:31
And now we can set button here,
startButton = We'll cast to a button.
3:34
Call find view by id and
type R.button nope and
3:40
Type R.id.startButton.
3:45
On the new line we can set
the onClickListener for it.
3:48
startbutton.setOnClickListener then we
want to type new onClickListener and
3:51
we get a View.onClickListener with
an onClick method that we can override.
3:56
Okay so in here we're going to get
the user's name which is text, so
4:02
let's create a string variable and
we'll name it name.
4:06
Now how can we set it, but we want to
call a method on our EditText variable.
4:11
So let's type name, field, and
type dot and take a look at auto complete.
4:15
Now you might be tempted to call something
like toString which is here at
4:20
the top of mine.
4:23
But that would just convert
the EditText object,
4:24
nameField, to its string representation,
which is not what we need.
4:26
That's not the user's name.
4:30
If you're curious what it looks like,
try it out and log it.
4:31
And take a look at what it prints.
4:33
We want to getText() from the EditText, so
select that method and add a semicolon.
4:35
But wait, we have an error.
4:41
If we hover over it, we see that
it's an incompatible types error.
4:43
It's requiring a string from the left, but
4:48
it found something called
android.text.Editable.
4:51
So, what kind of type is this and
how should we fix it?
4:54
Well, I know from experience, but
how would you figure this out on your own?
4:57
Well, besides watching this video.
5:00
Let's try to google it.
5:02
So let's phrase our problem.
5:04
We're trying to get text from edittext and
there it is from autocomplete,
5:05
and here is a few answers that
look like they might be helpful.
5:11
I'll click on this first one here
which is from stack overflow.
5:13
Okay, so this person is trying to
get the value of an edittext field.
5:16
And if we scroll down,
we can see a working example.
5:19
And this looks a lot like our own code.
5:23
So they've got an EditText here, and
down inside the onClickListener they're
5:25
calling getText, and then they're chaining
the method toString at the end of it.
5:30
Okay so what's happening is that
getText returns a different data type,
5:34
the editable data type.
5:38
And we need to convert an editable
string to a regular string.
5:40
So if we call .toString here,
it does the conversion for us.
5:44
And now we have the name in our variable.
5:48
Let's use a toast here to display it so
that we can test it out.
5:51
I'm old fashioned and I still like to
use toast messages for this purpose.
5:53
But you could certainly use something
else like a snack bar message
5:56
if you're more comfortable with that.
5:59
So on a new line,
let's type Toast.makeText.
6:00
For the context, let's pass in this,
then we'll pass in the name.
6:04
And then we need a length,
which we can use Toast.LENGTH_LONG.
6:07
At the end, we want to chain
the show method to make sure our
6:13
Toast actually shows up.
6:15
And we have an error.
6:17
Let's hover over it to see what it says.
6:19
So it says cannot resolve the method and
6:21
it gives us the parameter with the
different parameters that we passed in.
6:23
Now this is a common error which
is why I wanted to show here.
6:27
The problem is that we're dealing
with this anonymous inner class.
6:30
What that means is inside this
call to setOnClickListener,
6:34
we are creating a brand new mini class.
6:37
It's anonymous, meaning we have
not defined it in its own file or
6:41
given it a name.
6:43
Because of that the scope
of this variable changes.
6:45
It's now referring to this class,
this anonymous class,
6:49
instead of the overall activity class,
which is the context that we need.
6:52
This method now thinks that this is
a view.onClickListener instead of activity
6:57
and that's why we see it in
the error message down here.
7:01
It's an anonymous View at onClickListener.
7:04
So whenever we are in an anonymous
inner class like this,
7:07
we can reference the parent
activity by its full name.
7:10
So instead of just this,
if we type main activity.this,
7:14
we can now get the context
of the parent activity.
7:18
We use these anonymous inner
classes a lot in android.
7:21
They are useful because they let us
create an instance of an object like
7:24
this with certain extra details,
such as overloading the method.
7:27
This is generally more concise and easy to
understand in creating a new subclass or
7:30
new member variable.
7:35
But, it is up to you to use
whichever method you prefer.
7:36
Okay.
So our error is now resolved and
7:39
if we run the app,
it should build without any errors.
7:41
And now whatever we type,
7:45
in the edit text, if we tap on the button,
there we go, cool.
7:47
All right.
So this one example really applies to all
7:53
kinds of data input.
7:56
There might be different controls or
7:57
methods depending on the data, but
knowing how to use an EditText
7:58
means that you can easily figure out how
to use other types of input widgets.
8:02
Dates, for example,
use a different widget on a screen, but
8:06
the basic idea to get
the data is the same.
8:08
Coming up in the next section,
8:12
we'll see how to transfer that data to
a new activity, where we can use it
8:13
in our story, but because practice makes
perfect how about a little practice first?
8:16
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up