Alright, so in the last video we ended up
by changing the userInput and button
variables to vals instead of vars, and
consequently we were able to remove the
question marks and that's because they're
no longer needed, no longer needing
to be writable. So we can access the
properties then without using question
marks, and you can see that we did that
on lines 18 and 19, and then we
also made a change on line 26, with the user
Input.txt,
dot text. Okay, so what I've done there
is made both userInput and button local
variables in the onCreate function, and
that means that they're not available
anywhere else in the class, only inside
the onCreate function. We've left
TextView as a property, declared up at
the start of the class, so that it will
be available throughout the class. We're
going to see why soon, and that will also
explain why you might want to have
widget references as properties. So it's
worth actually running the program again
just to make sure that we haven't broken
anything, so let's quickly do that. I'm
just going to click on the button
without changing the name, you can see
that's still being appended, so that's
all working fine still. Now there's one
more change I want to make. When the
button's clicked,
you have to backspace it to remove the
text in the editText. So I'm just going
back and having a look there. So if we wanted to
change this, at the moment, we've got to
go back and change it before we can
actually change the text to make it
something different. So what we should
really be doing is clearing the contents
of the editText when the button is
clicked. Now an editText widget's text
property isn't a string, it's an editable.
Now we saw that in the previous video
when using the ctrl key, or command on a
Mac, and hovering over the userInput's
text property. So that means that this
isn't quite as easy as it may appear, but
ultimately, what we need to do is to set
out editText widget's text property to
an empty string, when the button's tapped.
Now if we go back and look at the code,
we were able to set the TextView widget's
text property on line 21,
you can see that here. So we used .text
and set that equal to two double quotes,
which is an empty string. We were able to
do that directly because a string can be
assigned to a TextView
widget's text property. However, the same
can't be done with an editText widget.
So how do we go about solving this? Well
there's actually two ways to do this, and
the change actually goes in the onClick
function, as that's the code that
executes when the user taps the button.
So we're going to come down here, after
doing the append, and we can type user
Input.text.clear parentheses.
So you can see there that I'm using the
clear function of the editable to clear
its contents. Now we can also use the set
Text method, instead of letting Kotlin
access the text property directly. So we
can come back here instead and comment
that line out, and I can instead, do
userInput.setText parentheses and
then two double quotes, which is an empty
string. Now if I use the ctrl command key,
depending on your operating system, to
hover over setText, you can see that it
takes a char sequence type there. So the
string type is compatible with Char
Sequence, so therefore a setText works
as we'd expected it to. Now there's very
little work to choose between these two
approaches, and so you can experiment by
commenting out each one in turn. When you
run the app, you'll see that they both
produce the behavior we want, and just to
confirm that I'm just going to run it
for this one with using the dot
setText. So I can press the button, noting that
the button's cleared now and I can type
Tim, press the button.
That's clearing each time, as you can see.
Alright, time for a mini-challenge. So your
challenge is that when the app starts,
the userInput field starts off
containing the text Name. So the user has
to backspace, as you saw me do, to delete
that before typing in the text they
really want. So this mini-challenge is to
get you to figure out how to remove that
default from the user input widget when
the app first starts. So go ahead and see
if you can figure that out.
Pause the video and come back when
you're ready to see the solution.
Alright, well there's several ways to
do this when programming. So the test
here, again, is does it work, and not that
you got exactly the same code as I'm
about to show you. So you could, for
example, have changed the layout so
there's no text in the editText widget.
That's a good solution and later in the
course, I'll be showing you a way to still
have text in a widget, without it
appearing when the app runs. For my
solution though, I'm going to clear the
text from userInput in the onCreate
method. Now you can also have used the
Clear method, as we saw earlier, so I'm
going to come up here. We've got the line
here, textView dot movementMethod equals
ScrollMovingMethod. After that, I'm
going to call userInput, or type in I
should say, userInput.setText and
double quotes in there. That's going to
be executed after that's been set up. Now
in the next video, I'm going to start by
discussing the activity lifecycle and
show you what went on with our edit
text when the device was rotated. So
let's actually try this out, and we can
see now that we've started out with no
text in the editText, which is correct,
and I can type in something, and it's still
being cleared each time. So that's
working.
Alright, so in the next video what I'm
going to do is discuss the activity
lifecycle and show you what actually
went on with our editext when the
device was rotated, which you saw a few
videos ago. Now by the way, you might find
I talk about methods rather than
functions in some of the videos coming
up. As Kotlin programmers, we're used to
talking about functions. Now in Java,
they're called methods, but don't worry
about the different names. They're
basically the same thing. Because the
lifecycle functions in the Android
framework are currently written in Java,
technically, they are methods rather than
functions. So I generally will use the
word method when talking about framework
functions. And sometimes I may get
confused and say method when I should
say function, and I think I've already
done that a few times in this course, or
vice-versa. But hopefully I won't do it
anymore but if I do, just keep in mind
that they're really the same thing here.
Alright, so what I'm going to do is end
by reformatting the code. So I'm going to
go ahead and do that, Code, Reformat. Al
right, so I'll see you in the next video.