Alright, so in the last video we created our Google, or we got our Google API key from the Google website.
We now need to add that to our Android Studio project.
Now we could declare the key as a top level constant, and there's no problem at all with doing that,
and in fact we've already seen how to do that.
So if you want to do it that way that's fine.
But I'm going to add it as a string resource
instead, and I've got two reasons for doing that.
The first and most important, is so that I can show you how to use string resources in your code.
We'll be using more of them later when we move away from hard coding strings in our layouts, and we'll actually
discuss the reasons for doing that when we do.
But this second reason is that it prevents me from having to obfuscate my keys on the videos.
Now this code could appear on screen at any time, and we may well miss it in the videos and forget
to obfuscate it.
So is that really a second reason or just a sneaky way of leading into the importance of protecting your keys.
Well OK maybe a bit of both.
Well Google have thought hard about the security, and even if someone has your API key, it'll be very
hard for them to spoof your app. They'd need to get their rogue app to have the same SHA1 fingerprint.
But the more information they have the easier it is.
So look after your keys and your SHA1 certificates. Treat them as you would a password.
But yes I know that mine leak out sometimes so, I delete them from the developer console after recording
the videos.
So if you suspect that your keys may have been compromised, then do the same.
It just takes seconds as you saw in the previous video, to generate a new key.
And that's the important reason for mentioning this.
The other reason, is that if you download my source code and try and run it, you'll need to replace my
Google API key with your own.
Sometimes my key gets left in the source code.
We're only human and make mistakes, but the key won't be valid,
and you'll actually get an error if you try to run my code. I'll show you the error later say so you'll know what to do about it.
Alright so I'm going to add my Google API key as a string resource.
So to do that we need to make sure we're in Android view,
open the app, and open the res folder,
and this time we're going to go into the values folder. Now there should only be a few files in here as
you can see, we've got three files in here.
Now I've just mentioned string resources,
so you might think that the key has to go into strings.xml, but that's not the case.
There's nothing special about the names in there,
and you can create files with any names,
remembering the rule about not using uppercase of course. But what's important are the XML tags,
not the filenames.
So it makes sense to call the string resource files string.xml,
but Android Studio actually doesn't care. When you build the project, all these files in this folder are compiled
into a single resource.
So we could add string resources into the colours file, but it wouldn't be a very sensible thing to do, but
it would work.
We can also create a completely new file and put it in there, and as we'll be looking at the string
files later, so that it appears on video, that's what we'll actually do.
So I'm going to come over here and right click on values and select new, then value resource, or values resource file.
Now as I said the filename's not important, to Android Studio anyway,
but I want to be able to find it again
so I'm going to call it keys, and leave everything else as default, and just press enter. And basically all these files
start the same way. After the usual XML declaration,
everything else is just contained within the resources tag.
Now we'll see that if we come over here to the strings.xml, there's the resources tag and a single
string tag in there. The XML declaration is missing
but Android Studio doesn't insist that there is one.
They are the same in the colours file if we have a look at that.
We have a few colour tags that define the basic colours for our app,
but we'll be looking at this in more detail, in a later section.
Now strangely this file does contain an XML declaration. Now as these files are only used by Android
Studio, and it knows what version and encoding it's used,
it's use in this declaration is not required.
Alright so let's go back to our keys.xml file. Our API
key will be a string, so we're going to create a new string tag.
So we're going to open this up, or put a new entry down here, in the resources, between the opening and closing resources
tag, and add a string tag, string name equals double quotes in uppercase GOOGLE underscore API underscore
KEY. And close that tag, and you want to paste in the API key that you copied from the developer console, and that's
our string resource defined. And you want to make sure that you haven't got any trailing or leading spaces in the
key that you've pasted in.
It should have the closing string tag, the opening string tag for the Google API key,
and the closing, and have the API key without any spaces either at the start or at the end.
Now very few resources are typed in capitals like this, but I think it looks better in the code this way.
But if you or your employer disagree with that, thats fine.
Use whatever naming convention you've settled on.
Alright so thats our resource defined.
Let's see if we can actually play a video. Now we're going to need a YouTube video ID so that we can
provide an ID for the video that we want to play.
And we'll need a constant for that, and we're also going to be playing a playlist later as well,
so we need to create two additional constants in YouTubeActivity. So then we'll go back and look up the
values for them. I'm going to make these top level constants,
so let's go to our YouTubeActivity, and we'll close down some of these other XML files now.
OK open our YouTubeActivity and we'll put these constants right at the top, so const
val YOUTUBE underscore VIDEO underscore ID equals TODO, then const val
YOUTUBE PLAYLIST equals TODO, well actually what we'll do is,
I mentioned I was going to add them as top level constants
so let's actually do that.
That should be above the class definition, like so.
Alright so what we need to do next now is go to YouTube and find a video.
So I'm going to switch and go back to YouTube.com, and I'm are going to paste in a link to one of my videos,
but you can use any video you like.
Alright and what we need is the part after the question mark v equals, so up here in the URL we can see watch
question mark v equals, so we need this part here after that,
just that component. Take a copy of that, and then go back to Android Studio and paste that in.
That's another YouTube video ID,so we've now got a valid YouTube video ID to play.
In other words our app's now knowing, or will know, which video to play. Now while I've got YouTube open in
my browser,
I'm actually going to grab a playlist ID.
Now, we won't need this til later, but it means I won't have to keep jumping around later in the section and we
can concentrate on the code. So if we go back to the browser, and again you can choose any playlist you like.
I'm just going to do a search for a Tim Buchalka playlist, because I know that I've got one, and I'm just going to
select this first one here,
but again you can use any playlist you want.
Click on that and I'll just stop it.
And actually a better way of doing it is to go back and click on View full playlist, so click on that, because
that then gives us the link. So you can see here this is this long list
after the playlist question mark list equals. Everything from that PL, right down to the end, I'm going to take
a copy of that, and then I'm going to go back to Android Studio and I'm also going to paste that, this time
into the YOUTUBE_PLAYLIST const. Alright, so that's it.
We've now got our YouTube video ID and our YOUTUBE_PLAYLIST set up, and
we've also added the key through our Google API to access the Google API so that we're able to play YouTube videos.
So let's end this video here. In the next one, now that we've got these things, we'll create the code for our
app to see a video working, so I'll see you in the next video.