Now in the previous video, we created the layouts for our two new activities.
And in this video we're going to add the code to make those activities work.
Now, we haven't yet got a class for the standalone activity, because we're adding the activity manually
instead of using the wizard.
So when I open the project pane if it's not open,
and then come on down to the actual package which is under the Java folder, right click, click on new and select
Kotlin File/Class, and we're going to call this one StandaloneActivity,
and under Kind, we're going to select Class instead of File, and click on OK.
Now this class is going to extend the AppcompatActivity class,
so I'm going to type after the class name, space colon.
And I'm going to type Appc, and that should be enough to bring up this list of classes, and I'm going to choose
AppCompatActivity from that list for the superclass, and I need to add parentheses on the end of that line.
Now we also want to implement an interface, and unlike Java, in Kotlin there's no implements keyword.
You just separate the class from the subsequent interfaces using a comma.
So what I'm going to do is type a comma, then after the comma I'm going to type in onClick, and we
get to the interface's box as you can see there, and we've got a couple of options there.
Now, we need to implement the view dot onClickListener, so make sure you select the right one from the
list. It should be already selected and you can see in my case the first one is selected, and IntelliSense
is very useful here, but you do really have to pay attention to what you're doing.
So we're not creating a dialogue interface so we don't want this second one,
so make sure you choose the correct one and press enter, and that adds that line for us.
Now that's our basic class created, and there's no functions added at the moment.
It is easy to do so, we just need to position the cursor inside the class,
in other words, inside the left and right curly braces, and use control O to choose the onCreate function
from the list. So control O, and you can just type the first few letters, onCreate, and there's onCreate, press enter,
and that implements it for us, and I'm just going to make an extra line of code there, or put
an extra line there. Now when we used the wizard to generate the YouTubeActivity for us,
the onCreate code was slightly different to what was generated here.
Now hopefully Google will have sorted this out and made them consistent, by the time you watch this.
But don't worry if you get slightly different code
as I have, because I'm going to be changing this code in a minute anyway, and you'll be able to make yours
match if necessary.
Now we've still got an error here at the moment, and that's relating to the fact that we haven't implemented
the onClick function yet, which is a required function for the onClickListener interface.
So let's put that right first.
So we're going to put a blank line after the onCreate function.
This time I'm going to do a control I to get the implements method's dialogue, and
I'm going to choose onClick there which is the only option that can be selected, and press enter.
Alright so at this point that's now cleared the error.
So what is this interface though we're implementing, and why are we doing it.
Well before I explain that, let's go back and add the code to the onCreate method because it'll make
more sense when you see the code in there.
So I'm going to start by leaving that super.onCreate line on line 14 as is, and on the next
line I'm going to do a setContentView, and it's going to be R.layout dot,
we're going to choose our activity standalone layout, activity_standalone. OK,
next line we're going to do btnPlayVideo.setOnClickListener, parentheses, this, and we're going to do the same
for the btnPlaylist, btnPlaylist.setOnClickListener,
this, and I did a capital L there, it should be a lower case l, that's better.
Alright so that's the onCreate function, and hopefully you should recognize that code, because it's almost
identical to the way we've been finding buttons in the layout, and setting them onClickListener so that
we can respond to the buttons being clicked. And Android Studio's added the synthetic import for us as
you can see there, on line 6.
Now there's a slight difference in the way that we've set the click listener this time though.
So I'm going to comment these two lines out for now, and review the two different ways that we've done
this previously. Let's go ahead and do that.
Now the first way is btnPlayVideo.setOnClickListener, in parentheses it was object colon View
dot On Click Listener, and then we need our left and right
curly braces, then we did an override control O, choosing onClick.
So that was the first, that's the approach we used in the button counter app in section 5,
and we can also implement that a different way, using a lambda expression. So I'm going to comment that one out, and
the other way of doing it, as I mentioned, was a lambda expression.
So that's btnPlayVideo.setOnClickListener,
and then in parentheses, view.OnClickListener, press enter there, and within the left and right curly braces we'll
put v then an arrow token, press enter, and then we're going to do our TODO, so TODO, we can actually copy
that entire line so let's just do that. Copy that line and then we'll uncomment it,
and that's now valid. Now that's a lot tidier because we don't need all that boilerplate code in colon,
and this is the code I'm referring to here, this override code on line 22.
Now that's fine if you're only using the listener with a single button. Typing all that in for each button's
a bit tedious though,
so what we can do is we can assign it to a variable, and use the same variable then for each button.
So I've commented out that first bit of code out, and let's review the approach we use, which is very similar
to what I've typed in the calculator app. Alright so I'll comment that out that, and the other thing we could've done, is we can assign
it to a variable and use the same variable for each button.
So we could do something like val listener equals view.OnClickListener, then we need our left and right curly brace, our braces,
and within the left and right curly braces, v arrow token, then we can paste in the code for the TODO, like
so, and clean that up. And then we could do btnPlayVideo.setOnClickListener, listener, and btn
Playlist.setOnClickListener, listener.
So here we basically created a new onClickListener object, and assign it to a variable,
and we can then pass that variable to the setOnClickListener method of as many buttons as we need.
Now what that code does is create an anonymous inner class, and assign an instance of that to a variable.
And that's the way we did it when we wanted to assign the same listener to several buttons in the calculator
app in section 6.
Now obviously the code had to work out which button was tapped, to do something different for each one,
but this does save having to create a new listener for each button.
Now a third approach we can take is to make our activity a listener.
Now if the activity implements the onClickListener interface, it can be passed whenever the onClickListener's
implementation's needed.
That's what I did originally when I passed this up here on lines 18 and 19, to
the button's setOnClickListener method, function. As long as our activity satisfies the interface,
so in other words it implements the onClick function, then we can use an instance of our activity whenever our
listener's needed. So I'm going to comment out at all this other code, leaving in just the original set dot
OnClickListeners, and uncomment these two. So all approaches here, all three approaches, pretty much do the same thing.
The advantage of the way we're using it now though, is that it moves the onClick code out of onCreate, and
this can make things more readable when the onClick function's quite long. It's not sort of messing
up the onCreate function in other words. So which approach should you use
here? Well it's really up to you as to which of the approaches you take, when you've got multiple buttons using
the same listener. If there's a lot of code in the onClick function, then implementing the interface and
using this can make the code more readable, as we're about to do.
And it also means that you can use instances of the class as a listener elsewhere in your code, to assign
it to buttons and other activities.
Now that's maybe not something you'd do with a class that extends activity or AppcompatActivity, but
it may be useful in other classes.
Alright, so we've got a listener attached to both of our buttons now, so we now need to write the code
to get it to do something.
Now what we want to do is launch Google's standalone player with the ID of a video to play, or the
ID of a playlist, and when we click the button it'll start the YouTube Player, almost as if you'd launched
it yourself from the phone's launch screen. Now it's slightly different to launching it manually, because we can also
tell it which video to start playing, but apart from that it's the same. Alright so the code is quite simple, so
I'm going to type it in then explain what's going on. So let's go down to the onClick function, and
we're going to remove this TODO now that because we are implementing it. And I'm going to start by typing val intent
equals when, then parentheses view.id, and our left and right curly braces,
and we need to change the name that was assigned. It's not really the best name there, p0,
we'll change that to view, and you're going to get rid of the question mark for now.
So within the code block for when, we're going to do R.id.btnPlayVideo arrow token, and that's going to be
YouTube, with a capital T, StandalonePlayer.createVideoIntent, then parentheses, and
we're going type 'this' as the first argument, comma space getString, then parentheses R.string dot
GOOGLE_API_KEY closing parentheses comma YOUTUBE VIDEO_ID, or YOUTUBE_VIDEO
_ID, then add our right, our closing parentheses. Alright so that's the first one.
Second one, we're going to do something very similar but this time for a playlist,
R.id.btnPlaylist arrow token, it's going to be YouTubeStandalonePlayer
dot, this time it's createPlaylistIntent, parentheses, and we'll start with this on the first line, comma space, and we'll
do you a get string again, parentheses R.string dot
GOOGLE_API_KEY again, parentheses comma space.
And then we'll do a YOUTUBE_PLAYLIST this time, YOUTUBE_PLAYIST in that line there, and then we're just going to add an else,
basically right down here, else arrow token again, and
we're going to throw IllegalArgumentException, and we'll put "Undefined button clicked". OK, then outside of
that code block we're going to put startActivity intent. Alright so to start an activity,
we use the start activity function and we pass it something called an intent.
So the code checks to see which button was tapped to call the onClick function, then sets the data for
an intent before calling startActivity.
Now we can use the playlist ID that we got earlier to play the playlist, and the video ID that we've
already defined in the YouTubeActivity class, to play a single video.
In both cases, we need to pass and use our Google API key,
and if you recall, both the ID's were declared as top level constants in YouTubeActivity, which means
we can use them without having to qualify them with a YouTubeActivity class name.
So in other words we added the definition above the class definition.
Now once you know what an intent is, the code's fairly easy to understand.
So let's actually have a look at the Google documentation for intents.
Alright, so we need to know a bit more about what an intent is, because once we actually figure that out you'll
find the code is pretty straightforward to understand.
So let's work on that in the next video.