1 00:00:05,290 --> 00:00:09,490 So one of the things I want to show you in this section, is how to launch a new activity from within 2 00:00:09,490 --> 00:00:13,980 your app. Now we're going to do that by clicking a button on the main screen, 3 00:00:13,980 --> 00:00:16,210 and we'll be adding the buttons later. 4 00:00:16,210 --> 00:00:19,150 For now though, we need to create a new activity 5 00:00:19,150 --> 00:00:25,330 so we've got one to launch. Now Android Studio makes creating a new activity easy, and it'll 6 00:00:25,330 --> 00:00:30,250 create both the code file and a layout file for you in one go, pretty much the same as the original 7 00:00:30,250 --> 00:00:33,520 wizard did when we created the project in the last video. 8 00:00:33,520 --> 00:00:39,490 So to do that we want to come over here and make sure the project pane is open, expand the Java folder, 9 00:00:39,490 --> 00:00:45,010 then right click the package name, choose new, and choose this time, 10 00:00:45,010 --> 00:00:47,530 we're going to choose activity from the context menu. 11 00:00:47,530 --> 00:00:51,700 Now there's quite a few options as you can see on the screen available, and these are basically the same 12 00:00:51,700 --> 00:00:55,090 as the templates that you get when creating a new project. 13 00:00:55,090 --> 00:00:58,570 In fact the first option gallery takes you to the same template gallery, 14 00:00:58,570 --> 00:01:04,310 so I'm going to click on that. Now we don't want a menu or the floating action button 15 00:01:04,310 --> 00:01:10,530 in this activity, so I'm going to choose the empty activity template and click on next. 16 00:01:10,530 --> 00:01:16,790 Now the names that Android Studio are suggesting aren't too good this time: Main2Activity and activity 17 00:01:16,790 --> 00:01:21,770 underscore main2 don't really describe what this activity's going to be for. 18 00:01:21,770 --> 00:01:28,670 So I'm going to call it YouTubeActivity. Come up here to the Main2 and change that to YouTube, and the Layout File 19 00:01:28,670 --> 00:01:30,470 activity_youtube. 20 00:01:30,470 --> 00:01:36,350 But you saw that as I changed the activity name, Android Studio helpfully renamed the Layout for us as well. 21 00:01:36,350 --> 00:01:38,840 So it just really saves us some typing which is pretty cool. 22 00:01:38,840 --> 00:01:42,800 Now the package name, you can't see that too well because of the resolution that I'm recording this in - 23 00:01:42,800 --> 00:01:45,890 I'll try and make that a little bit bigger - I don't it's going to help us there, 24 00:01:45,890 --> 00:01:50,150 but that's actually the package name. If I click on that we can see that now. 25 00:01:50,150 --> 00:01:52,850 So that's filled in for us and you wouldn't often want to change that, so 26 00:01:52,850 --> 00:01:54,950 I'm going to leave that as it is. 27 00:01:54,950 --> 00:01:59,510 Now the two check boxes are interesting though. We've already seen this backwards compatibility box when 28 00:01:59,510 --> 00:02:00,560 creating a new project, 29 00:02:00,560 --> 00:02:05,000 and we want to leave that checked so that the new activity will work on older devices. 30 00:02:05,000 --> 00:02:11,240 But this one here, this launcher activity check box, that's used to allow the activity we're creating, YouTube 31 00:02:11,240 --> 00:02:17,000 Activity in this case, to appear on the devices launch screen, so that we can run it from there just like 32 00:02:17,000 --> 00:02:18,630 you normally run apps. 33 00:02:18,630 --> 00:02:24,020 Now most apps will only have a single icon on the launch screen, and when you start an app on your phone 34 00:02:24,020 --> 00:02:27,140 it generally starts in the same place each time. 35 00:02:27,140 --> 00:02:31,850 But apps can have more than one icon though, and start up a different activity for each icon. 36 00:02:31,850 --> 00:02:37,310 Now an example of this would be an Android office type app, that provides a word processor in a spreadsheet 37 00:02:37,310 --> 00:02:41,180 program. Such an app could put two icons on the launch screen, 38 00:02:41,180 --> 00:02:47,060 so you can work with either the word processor or the spreadsheet by tapping the appropriate icon. 39 00:02:47,060 --> 00:02:51,830 Now we're going to look at how that works later in this section, and we'll see the changes to something called 40 00:02:51,830 --> 00:02:54,320 the manifest that make that all work. 41 00:02:54,320 --> 00:02:58,430 But in this case we don't want to launch the app directly into the player screen 42 00:02:58,430 --> 00:03:02,540 when we start it on a device, but we do want to run it while we're testing it. 43 00:03:02,540 --> 00:03:08,410 So I'm going to check that box, and we'll undo that change when we come to look at the manifest later. 44 00:03:08,410 --> 00:03:14,130 Now the final option might be off at the bottom of the screen, and you may have to scroll to see it. 45 00:03:14,130 --> 00:03:16,510 It's a Source Language, so here. 46 00:03:16,510 --> 00:03:20,350 Now it's possible to mix Java and Kotlin code in the same project. 47 00:03:20,350 --> 00:03:26,280 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, 48 00:03:26,280 --> 00:03:31,490 and you can say that Kotlin was selected anyway. 49 00:03:31,490 --> 00:03:34,670 I'll mention again that there's nothing magical about these templates. 50 00:03:34,670 --> 00:03:40,220 They're just a way of getting Androids Studio to do some of the work for us, using a wizard, instead of 51 00:03:40,220 --> 00:03:42,320 us doing it all manually. 52 00:03:42,320 --> 00:03:47,720 Alright so if I go ahead now and expand the res folder, in the layout folder, you can see we've got this 53 00:03:47,720 --> 00:03:54,350 activity_youtube.xml, and up here in our package we've got YouTubeActivity, a new class 54 00:03:54,350 --> 00:03:58,140 that's been created for us. And YouTubeActivity over here, 55 00:03:58,140 --> 00:04:02,840 it's put in the code to use the new layout when the activity's created, using setContentView - you can 56 00:04:02,840 --> 00:04:07,320 see that here on line 10, using the activity_youtube layout. 57 00:04:07,320 --> 00:04:09,960 Now there's nothing to stop us from creating this code as 58 00:04:09,960 --> 00:04:14,390 a new class, and creating the layouts separately as well. 59 00:04:14,390 --> 00:04:19,370 The Wizard just saves some typing for us and it makes sure that we don't forget to use the layout in the activity, 60 00:04:19,370 --> 00:04:26,770 for example. So at this point in time, our YoutubeActivity is just a normal activity, just like the ones we've created 61 00:04:26,770 --> 00:04:33,280 in earlier sections, and you can see here it's extending AppCompatActivity on line six. 62 00:04:33,280 --> 00:04:36,180 So it's compatible with earlier Android versions. 63 00:04:36,180 --> 00:04:39,280 Now to use it with the YouTubePlayer API, 64 00:04:39,280 --> 00:04:45,050 it actually needs to be a subclass of YouTubeBaseActivity, and it must also implement an interface 65 00:04:45,050 --> 00:04:50,170 that Google have defined for any activity that wants to use the API. 66 00:04:50,170 --> 00:04:53,980 So we need to go ahead and change the declaration of YouTubeActivity. 67 00:04:53,980 --> 00:04:57,880 So what I'm going to do first is come over here for AppCompatActivity. 68 00:04:57,880 --> 00:05:01,840 We're going to make that You Tube, You, Tube with a capital T, 69 00:05:01,840 --> 00:05:08,830 that's BaseActivity, like so, and notice that my updates, my imports, are being updated automatically because 70 00:05:08,830 --> 00:05:11,710 of the settings that I've defined in Android Studio. 71 00:05:11,710 --> 00:05:16,340 But you may be prompted to accept the imports if you didn't get it, or select, or set up Android 72 00:05:16,340 --> 00:05:19,300 Studio to automatically import classes. 73 00:05:19,300 --> 00:05:23,690 If that was the case and you haven't set it up, then the old AppCompatActivity imports should be showing 74 00:05:23,690 --> 00:05:25,950 in grey because it's not used anymore, 75 00:05:25,950 --> 00:05:29,650 but in my case you saw that Android Studio deleted that automatically as well. 76 00:05:29,650 --> 00:05:34,510 But you can delete it manually if for some reason yours, your Android Studio setup hasn't been configured 77 00:05:34,510 --> 00:05:35,880 for that. 78 00:05:35,880 --> 00:05:42,130 Alright now we also need to implement an interface, so let's add that to the class declaration as well. 79 00:05:42,130 --> 00:05:46,510 So we've got YouTubeActivity: then YouTubeBaseActivity, 80 00:05:46,510 --> 00:05:48,950 then we need to put a comma here, 81 00:05:48,950 --> 00:05:59,220 after the parentheses, then it's going to be YouTubePlayer, YouTubePlayer.OnInitializedListener, press enter. 82 00:05:59,220 --> 00:06:04,130 Then again accept any imports if you're prompted for that, and you could see now that I've done that, 83 00:06:04,130 --> 00:06:05,680 we're getting an error. 84 00:06:05,680 --> 00:06:10,370 And if you hover over that, it's basically saying that the 'class YouTubeActivity is not abstract, and 85 00:06:10,370 --> 00:06:17,540 doesn't implement the abstract member on initialised success'. Essentially we've said that the class implements the OnInitialized 86 00:06:17,540 --> 00:06:23,420 Listener interface but we haven't provided any implementation of the functions in that interface. 87 00:06:23,420 --> 00:06:26,810 Now think of an interface as a sort of contract. 88 00:06:26,810 --> 00:06:32,450 So if a class implements an interface, then what it's doing is it's guaranteeing that it will provide 89 00:06:32,450 --> 00:06:36,530 certain functions: the functions defined in the interface. 90 00:06:36,530 --> 00:06:43,550 So by doing this, anything that expects an object of the interface type knows that it can call these functions. 91 00:06:43,550 --> 00:06:46,940 Now an example of an interface in everyday use is a car. 92 00:06:46,940 --> 00:06:51,320 When you get into the driving seat of a car, you know that you can expect to find at least a steering 93 00:06:51,320 --> 00:06:55,400 wheel, an accelerator or gas pedal and a brake pedal. 94 00:06:55,400 --> 00:06:59,750 Now a more advanced version of that interface, or less advanced, depending on your point of view, 95 00:06:59,750 --> 00:07:05,510 may also include a clutch pedal and a gear stick. Now if you drive a car, you can jump into a dodgem 96 00:07:05,510 --> 00:07:08,990 car at a fairground and have a pretty good idea of what to expect, 97 00:07:08,990 --> 00:07:10,850 and the reverse is also true. 98 00:07:10,850 --> 00:07:15,270 If you can drive a fairground dodgem, then in theory at least, you should be able to set a street 99 00:07:15,270 --> 00:07:18,850 car in motion, even if that may not be very wise. 100 00:07:18,850 --> 00:07:24,620 Now a motor cycle performs pretty much the same function but has a completely different interface, and 101 00:07:24,620 --> 00:07:29,090 there's no guarantee that driving one enables you to cope with the controls of the other. 102 00:07:29,090 --> 00:07:34,550 So by implementing that interface, we're telling the YouTube API that it'll be able to call certain 103 00:07:34,550 --> 00:07:38,290 functions that our class will definitely provide. 104 00:07:38,290 --> 00:07:42,500 Now because this contract is totally binding, we actually get an error 105 00:07:42,500 --> 00:07:48,020 until we implement those methods. And Android Studio here is insisting that we do implement these methods, 106 00:07:48,020 --> 00:07:54,290 and the code won't actually compile until we do so. Now Android Studio knows which methods need to be implemented, 107 00:07:54,290 --> 00:07:57,920 and will create stubs for us so that we don't have to type them in, 108 00:07:57,920 --> 00:08:02,450 and thats also very useful because we don't have to worry about remembering their exact names or the 109 00:08:02,450 --> 00:08:04,970 arguments that they actually need. 110 00:08:04,970 --> 00:08:08,450 So I'm going to start a blank line after the uncreate here, 111 00:08:08,450 --> 00:08:14,120 then I'm going to use command n on my Mac, but it'll be Alt Insert on a PC. 112 00:08:14,120 --> 00:08:16,130 And I'm going to choose implement methods down here, 113 00:08:16,130 --> 00:08:19,280 and you can see that the shortcut for me was control I 114 00:08:19,280 --> 00:08:21,560 so I'll use that in the future. 115 00:08:21,560 --> 00:08:26,720 Now Android Studio's now automatically showing the methods that need to be implemented to satisfy this 116 00:08:26,720 --> 00:08:27,710 interface. 117 00:08:27,710 --> 00:08:34,309 So you make sure both these functions are selected and click on OK. You can see the error now disappears, and 118 00:08:34,309 --> 00:08:39,150 we've now got the basic code for both functions inserted into our class. 119 00:08:39,150 --> 00:08:42,330 Now I'm going to come back to these and explain what they're about in a minute. 120 00:08:42,330 --> 00:08:49,460 For now though I just want to clear the error, so that we can look at adding the YouTubePlayerView in our code. 121 00:08:49,460 --> 00:08:52,730 So we've seen how to to add widgets in our layout, so the layout designer, 122 00:08:52,730 --> 00:08:59,490 and this YouTube library comes with a YouTubePlayerView widget, that we can add to a layout to play videos. 123 00:08:59,490 --> 00:09:02,670 Now we're not going to add it to the layout using the layout designer though. 124 00:09:02,670 --> 00:09:04,700 We're going to do it all in code. 125 00:09:04,700 --> 00:09:07,640 Now there's a few reasons why I've decided to do that. 126 00:09:07,640 --> 00:09:13,790 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, 127 00:09:13,790 --> 00:09:16,880 and I'm going to quickly demonstrate the other two reasons. 128 00:09:16,880 --> 00:09:19,100 Don't follow along with this because we won't be doing it this way. 129 00:09:19,100 --> 00:09:23,690 I'm just going to use the layout designer to show you a couple of problems if we try to add the YouTube 130 00:09:23,690 --> 00:09:25,190 Player in the designer. 131 00:09:25,190 --> 00:09:29,150 So what I'm going to do is create a new layout file. I'm going to come to the layouts folder, 132 00:09:29,150 --> 00:09:35,180 right click and click on New, the Layout resource file, and I'll just call this test because I'm going to be deleting 133 00:09:35,180 --> 00:09:36,900 it later. 134 00:09:36,900 --> 00:09:41,640 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 135 00:09:41,640 --> 00:09:48,760 fine for purposes of out test. Click on OK. 136 00:09:48,760 --> 00:09:55,000 Now the first problem is that the YouTubePlayerView doesn't appear anywhere here in the list of widgets, 137 00:09:55,000 --> 00:10:00,400 even though we've added the API library to our project. The only way to add the YouTubePlayerView into 138 00:10:00,400 --> 00:10:02,530 the layout is to edit the Xml. 139 00:10:02,530 --> 00:10:07,530 And this was the same in previous versions of Android Studio as well. Widgets imported in the library 140 00:10:07,530 --> 00:10:09,040 don't appear in the palette. 141 00:10:09,040 --> 00:10:17,140 So what I'm going to do is come over here to the text, and I'm going to reformat this code. 142 00:10:17,140 --> 00:10:18,950 Alright so what I'm going to do now is paste in this 143 00:10:18,950 --> 00:10:22,980 code, so I'll come down here, and again you don't need to follow along with this. I'm just 144 00:10:22,980 --> 00:10:27,040 demonstrating a problem here so I'm going to paste in the declaration. 145 00:10:27,040 --> 00:10:30,400 Now when I go back and switch to the design tab, 146 00:10:30,400 --> 00:10:34,350 you can see we've got a problem. We get a rendering error - we get this problem here com.google at YouTube 147 00:10:34,350 --> 00:10:38,310 PlayerView. So the YouTubePlayer does appear in the layout, 148 00:10:38,310 --> 00:10:42,790 so it has been added, but the layout preview isn't working properly. 149 00:10:42,790 --> 00:10:46,630 Now this isn't as dramatic as what it was in previous versions of Android Studio. 150 00:10:46,630 --> 00:10:50,830 The error no longer pops up and fills half the layout window. 151 00:10:50,830 --> 00:10:54,170 To actually see the error we need to come over here and click on this button here, and 152 00:10:54,170 --> 00:10:58,030 you can see the rendering issue 'Failed to instantiate one or more classes'. 153 00:10:58,030 --> 00:11:03,430 So it's a lot less pronounced an error than what it was previously, but if we come over here and actually 154 00:11:03,430 --> 00:11:07,500 click on this little downs arrow to have a little bit more, get a bit more information, 155 00:11:07,500 --> 00:11:08,680 and pull this up a little bit, 156 00:11:08,680 --> 00:11:11,350 you can see the actual error we're actually getting here. 157 00:11:11,350 --> 00:11:18,060 So basically it's complaining that it can't instantiate the YouTubePlayerView class, and further on down if we 158 00:11:18,060 --> 00:11:18,760 have a bit of a read 159 00:11:18,760 --> 00:11:20,640 here, it actually says that a YouTubePlayer 160 00:11:20,640 --> 00:11:27,300 View can only be created with an activity which extends YouTubeBaseActivity as its context. Now our activity 161 00:11:27,300 --> 00:11:29,670 does extend YouTubeBaseActivity, 162 00:11:29,670 --> 00:11:35,070 so we should be able to fix that by specifying a context in the Xml. 163 00:11:35,070 --> 00:11:42,230 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. 164 00:11:42,230 --> 00:11:43,930 So we're going to type, the first one will be 165 00:11:43,930 --> 00:11:53,960 xmlns colon tools equals http:// and the one what we want is 166 00:11:53,960 --> 00:11:56,090 tools, this one here. 167 00:11:56,090 --> 00:12:04,970 So that's the first one, then on the next line we want tools context equals, and it's going to be academy dot 168 00:12:04,970 --> 00:12:11,480 learn programming, then what we want here is the youtubeplayer.YouTubeActivity so I'm going to select that. 169 00:12:11,480 --> 00:12:13,880 So that should have fixed the problem but if we come back to design 170 00:12:13,880 --> 00:12:20,460 view you see that we've still got the problem here. It's still not happy and we're still getting this instantiation error. 171 00:12:20,460 --> 00:12:23,310 So this is actually a bug in the rendering engine. 172 00:12:23,310 --> 00:12:27,000 Now the bug was also present in previous versions of Android Studio, and 173 00:12:27,000 --> 00:12:32,430 it's to do with the rendering engine not checking to see which context it should be using. 174 00:12:32,430 --> 00:12:35,100 Now the app will run fine. And the preview's 175 00:12:35,100 --> 00:12:39,450 now a lot better than it was in earlier versions, but it's still very confusing. 176 00:12:39,450 --> 00:12:44,640 So this was a good opportunity to see how we can add widgets to the layout in code, so that we can add 177 00:12:44,640 --> 00:12:47,520 extra widgets while our app is running if we wanted. 178 00:12:47,520 --> 00:12:52,380 Now it's really quite easy and although our app will use the YouTubePlayerView widget, you can actually use 179 00:12:52,380 --> 00:12:58,230 this technique to add any widgets you wish: text views, buttons, just about anything you want. 180 00:12:58,230 --> 00:13:01,410 In fact I'm going to start in the next video by adding a button. 181 00:13:01,410 --> 00:13:07,730 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 182 00:13:07,730 --> 00:13:12,130 because it was only a temporary thing anyway, delete, click on OK. 183 00:13:12,130 --> 00:13:14,380 Alright so let's finish the video here. 184 00:13:14,380 --> 00:13:19,280 In the next video we'll go back to the YouTubeActivity, and we'll start having a look and figuring out 185 00:13:19,280 --> 00:13:22,970 what we need to do to get hold of the layout object so that we can progress further. 186 00:13:22,970 --> 00:13:24,260 So I'll see you the next video.