let's go back now to our main activity as we continue one adding our extra
functionality for the button to do something when we click it
I want to have a look at this r class is all about we talked a little bit
about what it was and how it's a resource that's created automatically by Android
studio but let's have a look at the actual class itself just to see what it
looks like
so I'm just I'm just showing you all of this to explain what's going on here and
you wouldn't normally edit this class r and you certainly shouldn't go
making any changes to it
so if we open up what we need to do to see it we can't see it in our normal android
view we need to come up here and going into project view to even see the
file once we do that we are gonna open this up and come down here into app and
going to build generated source
r we come down here to debug and you should have your package name here in my
case it's com.
timbuchalka.buttonclickcounter i'm going to open that
and finally we get to the class r going to double click that to open it
now this is the resources class that is created automatically by Android studio
every time we make a change somewhere that involves using an ID
so once again there's no reason for you to edit this file and the actual fact
you can see that there's a warning at the top here saying that files under
the build folder are generated and should not be edited
what that means is that every time you recompile rebuild your application
these files are recreated so if it did make a change in here
the change would be lost but that's ok in this case because it is just a build
file and Android studio is responsible for keeping them updated so
as I scroll through it now you can see that it contains all all sorts of things
that probably don't make a lot of sense to you
there's a lot of stuff in there and all these weird numbers i'm going to search
for class ID
....and I hold down the command and press f you can also come up
here
click on find you do the equivalent thing i can't select that now because i
already got it open
that's how you find and you just type in what you want to search for and it will
automatically find it
so it's found this class ID you can see this file is quite long already up
to about 1900 lines here and scrolling down a little bit you can see the button here
edit text
and also text view as well
so these are the IDs that are internally android studio is associating with the
edit text that we defined in our layout because each ID needs to be unique but
fortunately from our point of view we just type in something that makes sense
to us and android create some sort of complicated number that it uses to make
it unique internally so most of these aren't used and they won't appear in the
final compiled version of the app but you can see that we go the ones
that we defined in our showing in there and obviously if we had more widgets
used on your layout or more what layouts for that matter and they will be
appearing in here
so these are actually in a class called ID which is inside another class called
r which is why closing this and switching back to the main activity
while we use r.ID.edit text
so its the main class r the class within a class .ID and then the actual
ID that we're using I know i've degrees a bit their but we're actually trying to
connect the button up to some codes
perhaps we should get back to that now so we've got some references to the
widgets
let's have a go at connecting this up so if you recall I said that the
android framework takes care of watching for user input and it notifies us when
there's something interesting happens now the way it notifies our code of are
events that have happened is by calling special methods that we write called
callbacks
so for example when the button is clicked the android framework will call
an on-click method if we've written one and we've told android that it's attached
to the particular button that was clicked
now we can't just go creating you know just any old method and call it on
click and expect java to be able to cope though java is a very strongly typed
language and that means that you have to provide exactly the right kind of object
or method that something expects now the way that that's defined in Java is using
interfaces and android users an interface called onclicklistener to make
sure that only the correct type of object is attached to the button to
respond to these clicks so once again i'm going to type the code and explain
on what its doing so come down here after our text view
and i'm going to type in private int num....
...then we are going to come down to the code under the text view under the last
line of code we've typed in the oncreate method I'm going to type
in some more code now the code is going to be
view...be absolutely sure you got the same case type as I'm
typing I'm going to press enter their notice when i did that we got a
import appear up on the screen on line 5 i'm going to continue typing by pressing
space then going to type in....
....
notice what happened when I type v we got this special options showing as
a menu here we've got these left brace and right brace so what that means
is it's going to be a special code that and Android studio will create for us if we
selected the you can either double click it or pressed enter so if I double click it
notice how it created this code for us automatically and what I'm going to do
is going to type in this code and then we're going to talk about it here going
type....
and notice what I'm typing how Android studio was trying to
find variables names for us and trying to shortcut them in code we don't have to type
if that's the right thing as I've mentioned before
press enter and automatically it adds that for us without us having to type it all
....
.....we need to come down here where this is
flagging a little error a little red error there and press the semicolon to
make that valid
alright so what does all that mean that I've typed in well firstly on line 15
i created an int variable called number of times clicked or numtimes clicked
and it's going to keep track of how many times we click the button in our
application now in our on create method
starting with this code on line 25 this line 25
i created a new object of type on click listener
now actually it's view.onclicklistener as you can see on the screen there
because the actual interface is defined in the view class now because all widgets
is extended view that all know about this interface so actually we can tap
things other than buttons and I'm sure if you run an application in the past
there's been many things that you been able to tap on the screen other
than buttons and get some sort of response from an Android application
so this is quite a simple method all it actually does down here in the on
click method
starting on line 28 its going to increase the value of num times clicked by one
and its going to add a message to our text view widget
now remember that we've now got a reference to the widget in the text
field
so what we're doing here is text view we're actually
calling in an append method to add a line of text to that text view when you see it
and when we run this application appear on the screen now this /n over here
on line 29 /n if you put that at the start of the message it causes a new
line to be started before the message is displayed and we're doing it that way so
that each message starts on a new line without that the message will display
one after the other
wrapping when they reached the edge of the text view so you can use the special
characters like /n and strings
whenever you want to insert a line break although it's more usual to put at
the end of a message rather than
beginning the last thing we need to do then is come down here and on line 34
what I'm going to do is type in.....
....so we are saying that we're telling the button that we
want to use our onclicklistener method that we've created whenever that button
is clicked
so when the button is clicked its going to call this on click method that we
have defined on line 27 is going to execute the code that we've typed in on
line 28 through a line 30
so this may look complicated but it's a standard pattern for attaching code to
an event so that your method is used when the event happens in other words
when the button is clicked in your application
so our code doesn't have to worry about checking the button
the on click method will be called for us when the button is clicked
so that is known as event driven programming and that is actually used by most
GUI frameworks including Microsoft Windows gnome or KDE on linux and the
mac OSX GUI as well so the pattern is very common now it can be
expressed in a slightly brief format using your something called an anonymous
class and brief are still using lambas and we will be looking at those but the
basic pattern here is the same
ok so we've typed in a lot of code time for a big test does this work
let's go ahead and run this application and see whether it actually works or not
going to come up here and click on the Run button
i'm going to choose my device okay i'm going to then tab over to it and wait
for it to start
okay there's our application and I'm just going to move this over a little bit so
we can still see our code now if you click it so i'm going to click on this button
now and now we got the button working as you can see so every time I
pressing the button here the application
this code is being executed the variables been incremented and we're
getting a result here
the button got tapped one time so you notice my emulator was already running
so if you've got a slower computer
it's probably a good idea to have the emulator running in the background
before you start programming or at least when you first start up android studio
to immediately start the emulator just save you a lot of frustrating waiting
you know perhaps minutes for the emulator to start in my case I've already
to have it running from a previous video and left it running and you can see
that it was fairly quick to actually build if we click and run this again
will get a more instant result but that first time I have to sort of build the
application and took a little bit longer to start
ok so that's the app running on the emulator we're going to finish the video
here now because in the next video we need to talk about a couple of things
that we really need explanation just in case you think you messed up
creating the layout in the challenge
so let's talk about that in the next video