1 00:00:05,380 --> 00:00:11,710 Alright, so in the last video we created our Google, or we got our Google API key from the Google website. 2 00:00:11,710 --> 00:00:14,890 We now need to add that to our Android Studio project. 3 00:00:14,890 --> 00:00:19,810 Now we could declare the key as a top level constant, and there's no problem at all with doing that, 4 00:00:19,810 --> 00:00:22,240 and in fact we've already seen how to do that. 5 00:00:22,240 --> 00:00:24,520 So if you want to do it that way that's fine. 6 00:00:24,520 --> 00:00:26,860 But I'm going to add it as a string resource 7 00:00:26,860 --> 00:00:29,920 instead, and I've got two reasons for doing that. 8 00:00:29,920 --> 00:00:35,470 The first and most important, is so that I can show you how to use string resources in your code. 9 00:00:35,470 --> 00:00:40,180 We'll be using more of them later when we move away from hard coding strings in our layouts, and we'll actually 10 00:00:40,180 --> 00:00:43,000 discuss the reasons for doing that when we do. 11 00:00:43,000 --> 00:00:48,220 But this second reason is that it prevents me from having to obfuscate my keys on the videos. 12 00:00:48,220 --> 00:00:52,690 Now this code could appear on screen at any time, and we may well miss it in the videos and forget 13 00:00:52,690 --> 00:00:54,220 to obfuscate it. 14 00:00:54,220 --> 00:00:59,350 So is that really a second reason or just a sneaky way of leading into the importance of protecting your keys. 15 00:00:59,350 --> 00:01:01,350 Well OK maybe a bit of both. 16 00:01:01,350 --> 00:01:07,420 Well Google have thought hard about the security, and even if someone has your API key, it'll be very 17 00:01:07,420 --> 00:01:13,690 hard for them to spoof your app. They'd need to get their rogue app to have the same SHA1 fingerprint. 18 00:01:13,690 --> 00:01:16,440 But the more information they have the easier it is. 19 00:01:16,440 --> 00:01:21,200 So look after your keys and your SHA1 certificates. Treat them as you would a password. 20 00:01:21,200 --> 00:01:26,500 But yes I know that mine leak out sometimes so, I delete them from the developer console after recording 21 00:01:26,500 --> 00:01:27,490 the videos. 22 00:01:27,490 --> 00:01:30,910 So if you suspect that your keys may have been compromised, then do the same. 23 00:01:30,910 --> 00:01:35,410 It just takes seconds as you saw in the previous video, to generate a new key. 24 00:01:35,410 --> 00:01:37,810 And that's the important reason for mentioning this. 25 00:01:37,810 --> 00:01:42,790 The other reason, is that if you download my source code and try and run it, you'll need to replace my 26 00:01:42,790 --> 00:01:45,280 Google API key with your own. 27 00:01:45,280 --> 00:01:47,710 Sometimes my key gets left in the source code. 28 00:01:47,710 --> 00:01:51,280 We're only human and make mistakes, but the key won't be valid, 29 00:01:51,280 --> 00:01:56,650 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. 30 00:01:56,650 --> 00:02:00,700 Alright so I'm going to add my Google API key as a string resource. 31 00:02:00,700 --> 00:02:03,870 So to do that we need to make sure we're in Android view, 32 00:02:03,870 --> 00:02:06,720 open the app, and open the res folder, 33 00:02:06,720 --> 00:02:11,050 and this time we're going to go into the values folder. Now there should only be a few files in here as 34 00:02:11,050 --> 00:02:13,330 you can see, we've got three files in here. 35 00:02:13,330 --> 00:02:15,670 Now I've just mentioned string resources, 36 00:02:15,670 --> 00:02:20,110 so you might think that the key has to go into strings.xml, but that's not the case. 37 00:02:20,110 --> 00:02:22,450 There's nothing special about the names in there, 38 00:02:22,450 --> 00:02:24,700 and you can create files with any names, 39 00:02:24,700 --> 00:02:30,370 remembering the rule about not using uppercase of course. But what's important are the XML tags, 40 00:02:30,370 --> 00:02:31,960 not the filenames. 41 00:02:31,960 --> 00:02:35,560 So it makes sense to call the string resource files string.xml, 42 00:02:35,560 --> 00:02:41,950 but Android Studio actually doesn't care. When you build the project, all these files in this folder are compiled 43 00:02:41,950 --> 00:02:43,390 into a single resource. 44 00:02:43,390 --> 00:02:48,760 So we could add string resources into the colours file, but it wouldn't be a very sensible thing to do, but 45 00:02:48,760 --> 00:02:49,930 it would work. 46 00:02:49,930 --> 00:02:54,370 We can also create a completely new file and put it in there, and as we'll be looking at the string 47 00:02:54,370 --> 00:02:58,140 files later, so that it appears on video, that's what we'll actually do. 48 00:02:58,140 --> 00:03:05,450 So I'm going to come over here and right click on values and select new, then value resource, or values resource file. 49 00:03:05,450 --> 00:03:09,130 Now as I said the filename's not important, to Android Studio anyway, 50 00:03:09,130 --> 00:03:10,640 but I want to be able to find it again 51 00:03:10,640 --> 00:03:18,290 so I'm going to call it keys, and leave everything else as default, and just press enter. And basically all these files 52 00:03:18,290 --> 00:03:22,100 start the same way. After the usual XML declaration, 53 00:03:22,100 --> 00:03:25,340 everything else is just contained within the resources tag. 54 00:03:25,340 --> 00:03:30,710 Now we'll see that if we come over here to the strings.xml, there's the resources tag and a single 55 00:03:30,710 --> 00:03:33,440 string tag in there. The XML declaration is missing 56 00:03:33,440 --> 00:03:35,870 but Android Studio doesn't insist that there is one. 57 00:03:35,870 --> 00:03:38,180 They are the same in the colours file if we have a look at that. 58 00:03:38,180 --> 00:03:41,360 We have a few colour tags that define the basic colours for our app, 59 00:03:41,360 --> 00:03:45,140 but we'll be looking at this in more detail, in a later section. 60 00:03:45,140 --> 00:03:51,260 Now strangely this file does contain an XML declaration. Now as these files are only used by Android 61 00:03:51,260 --> 00:03:54,770 Studio, and it knows what version and encoding it's used, 62 00:03:54,770 --> 00:03:57,740 it's use in this declaration is not required. 63 00:03:57,740 --> 00:04:01,130 Alright so let's go back to our keys.xml file. Our API 64 00:04:01,130 --> 00:04:05,140 key will be a string, so we're going to create a new string tag. 65 00:04:05,140 --> 00:04:10,280 So we're going to open this up, or put a new entry down here, in the resources, between the opening and closing resources 66 00:04:10,280 --> 00:04:20,329 tag, and add a string tag, string name equals double quotes in uppercase GOOGLE underscore API underscore 67 00:04:20,329 --> 00:04:27,720 KEY. And close that tag, and you want to paste in the API key that you copied from the developer console, and that's 68 00:04:27,720 --> 00:04:33,560 our string resource defined. And you want to make sure that you haven't got any trailing or leading spaces in the 69 00:04:33,560 --> 00:04:35,000 key that you've pasted in. 70 00:04:35,000 --> 00:04:39,950 It should have the closing string tag, the opening string tag for the Google API key, 71 00:04:39,950 --> 00:04:44,960 and the closing, and have the API key without any spaces either at the start or at the end. 72 00:04:44,960 --> 00:04:50,570 Now very few resources are typed in capitals like this, but I think it looks better in the code this way. 73 00:04:50,570 --> 00:04:53,720 But if you or your employer disagree with that, thats fine. 74 00:04:53,720 --> 00:04:56,540 Use whatever naming convention you've settled on. 75 00:04:56,540 --> 00:04:59,060 Alright so thats our resource defined. 76 00:04:59,060 --> 00:05:03,740 Let's see if we can actually play a video. Now we're going to need a YouTube video ID so that we can 77 00:05:03,740 --> 00:05:06,680 provide an ID for the video that we want to play. 78 00:05:06,680 --> 00:05:10,850 And we'll need a constant for that, and we're also going to be playing a playlist later as well, 79 00:05:10,850 --> 00:05:16,370 so we need to create two additional constants in YouTubeActivity. So then we'll go back and look up the 80 00:05:16,370 --> 00:05:18,620 values for them. I'm going to make these top level constants, 81 00:05:18,620 --> 00:05:26,850 so let's go to our YouTubeActivity, and we'll close down some of these other XML files now. 82 00:05:26,850 --> 00:05:33,460 OK open our YouTubeActivity and we'll put these constants right at the top, so const 83 00:05:33,460 --> 00:05:46,750 val YOUTUBE underscore VIDEO underscore ID equals TODO, then const val 84 00:05:46,750 --> 00:05:54,310 YOUTUBE PLAYLIST equals TODO, well actually what we'll do is, 85 00:05:54,310 --> 00:05:57,120 I mentioned I was going to add them as top level constants 86 00:05:57,120 --> 00:05:59,260 so let's actually do that. 87 00:05:59,260 --> 00:06:04,920 That should be above the class definition, like so. 88 00:06:04,920 --> 00:06:08,640 Alright so what we need to do next now is go to YouTube and find a video. 89 00:06:08,640 --> 00:06:16,160 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, 90 00:06:16,160 --> 00:06:19,220 but you can use any video you like. 91 00:06:19,220 --> 00:06:25,960 Alright and what we need is the part after the question mark v equals, so up here in the URL we can see watch 92 00:06:25,960 --> 00:06:29,140 question mark v equals, so we need this part here after that, 93 00:06:29,140 --> 00:06:36,710 just that component. Take a copy of that, and then go back to Android Studio and paste that in. 94 00:06:36,710 --> 00:06:42,010 That's another YouTube video ID,so we've now got a valid YouTube video ID to play. 95 00:06:42,010 --> 00:06:47,650 In other words our app's now knowing, or will know, which video to play. Now while I've got YouTube open in 96 00:06:47,650 --> 00:06:48,730 my browser, 97 00:06:48,730 --> 00:06:51,370 I'm actually going to grab a playlist ID. 98 00:06:51,370 --> 00:06:55,480 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 99 00:06:55,480 --> 00:07:01,750 can concentrate on the code. So if we go back to the browser, and again you can choose any playlist you like. 100 00:07:01,750 --> 00:07:08,380 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 101 00:07:08,380 --> 00:07:09,400 select this first one here, 102 00:07:09,400 --> 00:07:12,160 but again you can use any playlist you want. 103 00:07:12,160 --> 00:07:14,810 Click on that and I'll just stop it. 104 00:07:14,810 --> 00:07:20,020 And actually a better way of doing it is to go back and click on View full playlist, so click on that, because 105 00:07:20,020 --> 00:07:22,980 that then gives us the link. So you can see here this is this long list 106 00:07:22,980 --> 00:07:28,990 after the playlist question mark list equals. Everything from that PL, right down to the end, I'm going to take 107 00:07:28,990 --> 00:07:34,300 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 108 00:07:34,300 --> 00:07:38,020 into the YOUTUBE_PLAYLIST const. Alright, so that's it. 109 00:07:38,020 --> 00:07:42,380 We've now got our YouTube video ID and our YOUTUBE_PLAYLIST set up, and 110 00:07:42,380 --> 00:07:48,820 we've also added the key through our Google API to access the Google API so that we're able to play YouTube videos. 111 00:07:48,820 --> 00:07:53,950 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 112 00:07:53,950 --> 00:07:56,840 app to see a video working, so I'll see you in the next video.