So one of the things I want to show you in this section, is how to launch a new activity from within
your app. Now we're going to do that by clicking a button on the main screen,
and we'll be adding the buttons later.
For now though, we need to create a new activity
so we've got one to launch. Now Android Studio makes creating a new activity easy, and it'll
create both the code file and a layout file for you in one go, pretty much the same as the original
wizard did when we created the project in the last video.
So to do that we want to come over here and make sure the project pane is open, expand the Java folder,
then right click the package name, choose new, and choose this time,
we're going to choose activity from the context menu.
Now there's quite a few options as you can see on the screen available, and these are basically the same
as the templates that you get when creating a new project.
In fact the first option gallery takes you to the same template gallery,
so I'm going to click on that. Now we don't want a menu or the floating action button
in this activity, so I'm going to choose the empty activity template and click on next.
Now the names that Android Studio are suggesting aren't too good this time: Main2Activity and activity
underscore main2 don't really describe what this activity's going to be for.
So I'm going to call it YouTubeActivity. Come up here to the Main2 and change that to YouTube, and the Layout File
activity_youtube.
But you saw that as I changed the activity name, Android Studio helpfully renamed the Layout for us as well.
So it just really saves us some typing which is pretty cool.
Now the package name, you can't see that too well because of the resolution that I'm recording this in -
I'll try and make that a little bit bigger - I don't it's going to help us there,
but that's actually the package name. If I click on that we can see that now.
So that's filled in for us and you wouldn't often want to change that, so
I'm going to leave that as it is.
Now the two check boxes are interesting though. We've already seen this backwards compatibility box when
creating a new project,
and we want to leave that checked so that the new activity will work on older devices.
But this one here, this launcher activity check box, that's used to allow the activity we're creating, YouTube
Activity in this case, to appear on the devices launch screen, so that we can run it from there just like
you normally run apps.
Now most apps will only have a single icon on the launch screen, and when you start an app on your phone
it generally starts in the same place each time.
But apps can have more than one icon though, and start up a different activity for each icon.
Now an example of this would be an Android office type app, that provides a word processor in a spreadsheet
program. Such an app could put two icons on the launch screen,
so you can work with either the word processor or the spreadsheet by tapping the appropriate icon.
Now we're going to look at how that works later in this section, and we'll see the changes to something called
the manifest that make that all work.
But in this case we don't want to launch the app directly into the player screen
when we start it on a device, but we do want to run it while we're testing it.
So I'm going to check that box, and we'll undo that change when we come to look at the manifest later.
Now the final option might be off at the bottom of the screen, and you may have to scroll to see it.
It's a Source Language, so here.
Now it's possible to mix Java and Kotlin code in the same project.
So you could choose Java for this activity if you wanted. I'm going to leave it though set to Kotlin, and click on finish,
and you can say that Kotlin was selected anyway.
I'll mention again that there's nothing magical about these templates.
They're just a way of getting Androids Studio to do some of the work for us, using a wizard, instead of
us doing it all manually.
Alright so if I go ahead now and expand the res folder, in the layout folder, you can see we've got this
activity_youtube.xml, and up here in our package we've got YouTubeActivity, a new class
that's been created for us. And YouTubeActivity over here,
it's put in the code to use the new layout when the activity's created, using setContentView - you can
see that here on line 10, using the activity_youtube layout.
Now there's nothing to stop us from creating this code as
a new class, and creating the layouts separately as well.
The Wizard just saves some typing for us and it makes sure that we don't forget to use the layout in the activity,
for example. So at this point in time, our YoutubeActivity is just a normal activity, just like the ones we've created
in earlier sections, and you can see here it's extending AppCompatActivity on line six.
So it's compatible with earlier Android versions.
Now to use it with the YouTubePlayer API,
it actually needs to be a subclass of YouTubeBaseActivity, and it must also implement an interface
that Google have defined for any activity that wants to use the API.
So we need to go ahead and change the declaration of YouTubeActivity.
So what I'm going to do first is come over here for AppCompatActivity.
We're going to make that You Tube, You, Tube with a capital T,
that's BaseActivity, like so, and notice that my updates, my imports, are being updated automatically because
of the settings that I've defined in Android Studio.
But you may be prompted to accept the imports if you didn't get it, or select, or set up Android
Studio to automatically import classes.
If that was the case and you haven't set it up, then the old AppCompatActivity imports should be showing
in grey because it's not used anymore,
but in my case you saw that Android Studio deleted that automatically as well.
But you can delete it manually if for some reason yours, your Android Studio setup hasn't been configured
for that.
Alright now we also need to implement an interface, so let's add that to the class declaration as well.
So we've got YouTubeActivity: then YouTubeBaseActivity,
then we need to put a comma here,
after the parentheses, then it's going to be YouTubePlayer, YouTubePlayer.OnInitializedListener, press enter.
Then again accept any imports if you're prompted for that, and you could see now that I've done that,
we're getting an error.
And if you hover over that, it's basically saying that the 'class YouTubeActivity is not abstract, and
doesn't implement the abstract member on initialised success'. Essentially we've said that the class implements the OnInitialized
Listener interface but we haven't provided any implementation of the functions in that interface.
Now think of an interface as a sort of contract.
So if a class implements an interface, then what it's doing is it's guaranteeing that it will provide
certain functions: the functions defined in the interface.
So by doing this, anything that expects an object of the interface type knows that it can call these functions.
Now an example of an interface in everyday use is a car.
When you get into the driving seat of a car, you know that you can expect to find at least a steering
wheel, an accelerator or gas pedal and a brake pedal.
Now a more advanced version of that interface, or less advanced, depending on your point of view,
may also include a clutch pedal and a gear stick. Now if you drive a car, you can jump into a dodgem
car at a fairground and have a pretty good idea of what to expect,
and the reverse is also true.
If you can drive a fairground dodgem, then in theory at least, you should be able to set a street
car in motion, even if that may not be very wise.
Now a motor cycle performs pretty much the same function but has a completely different interface, and
there's no guarantee that driving one enables you to cope with the controls of the other.
So by implementing that interface, we're telling the YouTube API that it'll be able to call certain
functions that our class will definitely provide.
Now because this contract is totally binding, we actually get an error
until we implement those methods. And Android Studio here is insisting that we do implement these methods,
and the code won't actually compile until we do so. Now Android Studio knows which methods need to be implemented,
and will create stubs for us so that we don't have to type them in,
and thats also very useful because we don't have to worry about remembering their exact names or the
arguments that they actually need.
So I'm going to start a blank line after the uncreate here,
then I'm going to use command n on my Mac, but it'll be Alt Insert on a PC.
And I'm going to choose implement methods down here,
and you can see that the shortcut for me was control I
so I'll use that in the future.
Now Android Studio's now automatically showing the methods that need to be implemented to satisfy this
interface.
So you make sure both these functions are selected and click on OK. You can see the error now disappears, and
we've now got the basic code for both functions inserted into our class.
Now I'm going to come back to these and explain what they're about in a minute.
For now though I just want to clear the error, so that we can look at adding the YouTubePlayerView in our code.
So we've seen how to to add widgets in our layout, so the layout designer,
and this YouTube library comes with a YouTubePlayerView widget, that we can add to a layout to play videos.
Now we're not going to add it to the layout using the layout designer though.
We're going to do it all in code.
Now there's a few reasons why I've decided to do that.
It's easier for one thing, but it's also a good chance to show you how to add a widget to a layout in code,
and I'm going to quickly demonstrate the other two reasons.
Don't follow along with this because we won't be doing it this way.
I'm just going to use the layout designer to show you a couple of problems if we try to add the YouTube
Player in the designer.
So what I'm going to do is create a new layout file. I'm going to come to the layouts folder,
right click and click on New, the Layout resource file, and I'll just call this test because I'm going to be deleting
it later.
Now whatever the root element is, I'm going to leave it as what it is. At the moment it's ConstraintLayout and that's
fine for purposes of out test. Click on OK.
Now the first problem is that the YouTubePlayerView doesn't appear anywhere here in the list of widgets,
even though we've added the API library to our project. The only way to add the YouTubePlayerView into
the layout is to edit the Xml.
And this was the same in previous versions of Android Studio as well. Widgets imported in the library
don't appear in the palette.
So what I'm going to do is come over here to the text, and I'm going to reformat this code.
Alright so what I'm going to do now is paste in this
code, so I'll come down here, and again you don't need to follow along with this. I'm just
demonstrating a problem here so I'm going to paste in the declaration.
Now when I go back and switch to the design tab,
you can see we've got a problem. We get a rendering error - we get this problem here com.google at YouTube
PlayerView. So the YouTubePlayer does appear in the layout,
so it has been added, but the layout preview isn't working properly.
Now this isn't as dramatic as what it was in previous versions of Android Studio.
The error no longer pops up and fills half the layout window.
To actually see the error we need to come over here and click on this button here, and
you can see the rendering issue 'Failed to instantiate one or more classes'.
So it's a lot less pronounced an error than what it was previously, but if we come over here and actually
click on this little downs arrow to have a little bit more, get a bit more information,
and pull this up a little bit,
you can see the actual error we're actually getting here.
So basically it's complaining that it can't instantiate the YouTubePlayerView class, and further on down if we
have a bit of a read
here, it actually says that a YouTubePlayer
View can only be created with an activity which extends YouTubeBaseActivity as its context. Now our activity
does extend YouTubeBaseActivity,
so we should be able to fix that by specifying a context in the Xml.
So if we go back to our text view, and if we add those two lines. We want to add them just after this declaration up here.
So we're going to type, the first one will be
xmlns colon tools equals http:// and the one what we want is
tools, this one here.
So that's the first one, then on the next line we want tools context equals, and it's going to be academy dot
learn programming, then what we want here is the youtubeplayer.YouTubeActivity so I'm going to select that.
So that should have fixed the problem but if we come back to design
view you see that we've still got the problem here. It's still not happy and we're still getting this instantiation error.
So this is actually a bug in the rendering engine.
Now the bug was also present in previous versions of Android Studio, and
it's to do with the rendering engine not checking to see which context it should be using.
Now the app will run fine. And the preview's
now a lot better than it was in earlier versions, but it's still very confusing.
So this was a good opportunity to see how we can add widgets to the layout in code, so that we can add
extra widgets while our app is running if we wanted.
Now it's really quite easy and although our app will use the YouTubePlayerView widget, you can actually use
this technique to add any widgets you wish: text views, buttons, just about anything you want.
In fact I'm going to start in the next video by adding a button.
But for now though I'm going to come over here and close this layout firstly. Then we're going to delete it over here
because it was only a temporary thing anyway, delete, click on OK.
Alright so let's finish the video here.
In the next video we'll go back to the YouTubeActivity, and we'll start having a look and figuring out
what we need to do to get hold of the layout object so that we can progress further.
So I'll see you the next video.