1 00:00:04,730 --> 00:00:06,319 So I mentioned at the end of the last 2 00:00:06,319 --> 00:00:07,609 video that we were going to create 3 00:00:07,609 --> 00:00:09,379 another project, so I'm going to close 4 00:00:09,379 --> 00:00:10,879 this project by going up to the file 5 00:00:10,879 --> 00:00:14,150 menu and selecting close project and 6 00:00:14,150 --> 00:00:15,169 what we're going to do is create another 7 00:00:15,169 --> 00:00:17,450 project this time based on that other 8 00:00:17,450 --> 00:00:19,939 template the basic activity template. 9 00:00:19,939 --> 00:00:21,619 So I'm going to a click on start new 10 00:00:21,619 --> 00:00:24,020 Android studio project, so once again I'm 11 00:00:24,020 --> 00:00:25,399 going to accept all the default values 12 00:00:25,399 --> 00:00:28,070 for everything and uncheck the include 13 00:00:28,070 --> 00:00:30,200 Cochrane support box but if you're using 14 00:00:30,200 --> 00:00:32,659 Kotlin, this time keep it ticked so we're 15 00:00:32,659 --> 00:00:34,730 going to click on that checkbox now, so 16 00:00:34,730 --> 00:00:36,890 that you can see the difference so click 17 00:00:36,890 --> 00:00:38,900 on next then we're going to go to next 18 00:00:38,900 --> 00:00:40,760 again and then with this time we're 19 00:00:40,760 --> 00:00:43,280 going to choose the basic activity once 20 00:00:43,280 --> 00:00:44,270 we've done that we're going to click 21 00:00:44,270 --> 00:00:46,880 next. So once we get here we get to name 22 00:00:46,880 --> 00:00:48,530 the activity in layout but now this is a 23 00:00:48,530 --> 00:00:50,810 couple of extra options compared to the 24 00:00:50,810 --> 00:00:53,829 last video and we use the empty activity 25 00:00:53,829 --> 00:00:56,360 the title will be shown on the toolbar 26 00:00:56,360 --> 00:00:58,250 and this template adds to the project 27 00:00:58,250 --> 00:01:00,380 but note that the activity name and 28 00:01:00,380 --> 00:01:01,940 layout name are the same as for the 29 00:01:01,940 --> 00:01:03,950 empty activity template, so we're going 30 00:01:03,950 --> 00:01:05,710 to click on finish at this point and 31 00:01:05,710 --> 00:01:08,060 once again Android studio should create 32 00:01:08,060 --> 00:01:10,189 a basic project based on the template we 33 00:01:10,189 --> 00:01:12,289 selected. Now by the way while it's 34 00:01:12,289 --> 00:01:13,759 building the project it's not uncommon 35 00:01:13,759 --> 00:01:15,829 to see errors on the screen but they 36 00:01:15,829 --> 00:01:17,179 should clear when everything is finished 37 00:01:17,179 --> 00:01:19,249 just keep an eye down here in the bottom 38 00:01:19,249 --> 00:01:21,649 right hand corner and the status bar and 39 00:01:21,649 --> 00:01:23,270 don't try to do anything with a project 40 00:01:23,270 --> 00:01:24,020 until it's finished 41 00:01:24,020 --> 00:01:25,909 it'll say something like Gradle build 42 00:01:25,909 --> 00:01:28,459 finished in or words to that effect once 43 00:01:28,459 --> 00:01:29,990 it's finished and how long it took when 44 00:01:29,990 --> 00:01:31,639 it's done you can see don't hit the 45 00:01:31,639 --> 00:01:34,399 moment it says Gradle build running you 46 00:01:34,399 --> 00:01:35,539 can see down the bottom left hand corner 47 00:01:35,539 --> 00:01:38,179 of Gradle build finished and we're good 48 00:01:38,179 --> 00:01:40,340 to go now by the way you may also see a 49 00:01:40,340 --> 00:01:41,810 message at the top of the screen about 50 00:01:41,810 --> 00:01:44,209 Gradle files haven't changed this 51 00:01:44,209 --> 00:01:45,679 happens from time to time with some 52 00:01:45,679 --> 00:01:48,109 Android studio versions so if you do see 53 00:01:48,109 --> 00:01:50,689 that click the sync now button link at 54 00:01:50,689 --> 00:01:52,009 the right hand side as you can see on 55 00:01:52,009 --> 00:01:53,929 the screen there when Android studio is 56 00:01:53,929 --> 00:01:55,700 finished creating the project you should 57 00:01:55,700 --> 00:01:57,439 see main activity and a layout called 58 00:01:57,439 --> 00:01:59,569 content underscore main dot xml we can 59 00:01:59,569 --> 00:02:01,609 see them both on the screen up here now 60 00:02:01,609 --> 00:02:03,139 the layout looks very similar to our 61 00:02:03,139 --> 00:02:04,849 empty activity layout from the last 62 00:02:04,849 --> 00:02:07,099 video but there's also a floating action 63 00:02:07,099 --> 00:02:09,860 button down here so we can see at the 64 00:02:09,860 --> 00:02:12,260 bottom right. Now if you feeling chased 65 00:02:12,260 --> 00:02:13,489 hit because the image promised you a 66 00:02:13,489 --> 00:02:16,099 menu don't worry it's there but it 67 00:02:16,099 --> 00:02:17,730 doesn't appear on this layout 68 00:02:17,730 --> 00:02:19,739 now one difference between this template 69 00:02:19,739 --> 00:02:21,900 and the empty activity one is that 70 00:02:21,900 --> 00:02:24,000 Google splits the layouts for each 71 00:02:24,000 --> 00:02:26,629 activity up into two different files, 72 00:02:26,629 --> 00:02:29,040 content underscore main dot xml here is 73 00:02:29,040 --> 00:02:31,050 the one that would be using to put our 74 00:02:31,050 --> 00:02:33,209 own widgets into things like buttons and 75 00:02:33,209 --> 00:02:34,830 text boxes and anything else that we 76 00:02:34,830 --> 00:02:37,170 want displayed on the screen and the 77 00:02:37,170 --> 00:02:38,550 floating action button live in another 78 00:02:38,550 --> 00:02:40,800 file called activity underscore main dot 79 00:02:40,800 --> 00:02:42,510 xml, you notice over here we've got that 80 00:02:42,510 --> 00:02:44,549 file so we double click that to open it 81 00:02:44,549 --> 00:02:46,530 so you can see that the toolbar the 82 00:02:46,530 --> 00:02:48,209 floating action button can be selected 83 00:02:48,209 --> 00:02:49,440 when I click on them so I can click on 84 00:02:49,440 --> 00:02:50,910 something and notice that while I'm 85 00:02:50,910 --> 00:02:52,650 doing that it's selected but if I go 86 00:02:52,650 --> 00:02:54,569 back to content underscore main dot XML 87 00:02:54,569 --> 00:02:57,239 I can't click the floating action button 88 00:02:57,239 --> 00:02:58,860 and that's because they don't exist in 89 00:02:58,860 --> 00:03:00,540 this fall although Android studio does 90 00:03:00,540 --> 00:03:02,670 display the floating action button in 91 00:03:02,670 --> 00:03:04,950 the preview now as I click on the 92 00:03:04,950 --> 00:03:06,720 toolbar and the floating action button 93 00:03:06,720 --> 00:03:09,569 back in activity underscore main those 94 00:03:09,569 --> 00:03:11,069 heather's selected over the far left of 95 00:03:11,069 --> 00:03:12,329 the screen over here in the component 96 00:03:12,329 --> 00:03:15,090 tree and when I click on the main part 97 00:03:15,090 --> 00:03:16,799 of the screen here it selects the 98 00:03:16,799 --> 00:03:18,420 include line which is including the 99 00:03:18,420 --> 00:03:20,819 layout called content underscore main 100 00:03:20,819 --> 00:03:22,230 and you can see that over here to the 101 00:03:22,230 --> 00:03:23,940 right-hand side and this is called the 102 00:03:23,940 --> 00:03:26,310 attributes pane, so the idea is that the 103 00:03:26,310 --> 00:03:28,709 layered is split up to keep your widgets 104 00:03:28,709 --> 00:03:30,480 in a separate file from the toolbar and 105 00:03:30,480 --> 00:03:33,180 the floating action button, if you wanted 106 00:03:33,180 --> 00:03:34,950 to you could delete the include line and 107 00:03:34,950 --> 00:03:36,780 place all your widgets in here but it's 108 00:03:36,780 --> 00:03:39,870 less confusing to keep them separate now 109 00:03:39,870 --> 00:03:41,730 Google introduces separations back in 110 00:03:41,730 --> 00:03:44,220 Android studio 1.5 and that caused 111 00:03:44,220 --> 00:03:46,139 students at the time of some confusion 112 00:03:46,139 --> 00:03:47,850 well they suddenly found two layout 113 00:03:47,850 --> 00:03:49,980 files in their projects now it's 114 00:03:49,980 --> 00:03:51,480 possible that content underscore main 115 00:03:51,480 --> 00:03:53,459 may disappear from the template in a 116 00:03:53,459 --> 00:03:55,200 future version but now that you know 117 00:03:55,200 --> 00:03:56,639 that content underscore main is just 118 00:03:56,639 --> 00:03:58,470 included in activity in the school main 119 00:03:58,470 --> 00:04:00,180 and that you don't have to keep you 120 00:04:00,180 --> 00:04:02,160 widget separate if you don't want to you 121 00:04:02,160 --> 00:04:03,540 shouldn't have any problems if things do 122 00:04:03,540 --> 00:04:05,730 change and incidentally in this version 123 00:04:05,730 --> 00:04:07,859 of Android studio the toolbars blank 124 00:04:07,859 --> 00:04:10,170 even when previewing in it even when 125 00:04:10,170 --> 00:04:11,639 previewing activity underscore main 126 00:04:11,639 --> 00:04:14,190 and that does sometimes happen but if I 127 00:04:14,190 --> 00:04:16,470 run the app the toolbar would appear, 128 00:04:16,470 --> 00:04:18,238 so if you get a blank toolbar in preview 129 00:04:18,238 --> 00:04:20,039 mode don't worry about it it will show 130 00:04:20,039 --> 00:04:22,950 up when the app is running. Alright now 131 00:04:22,950 --> 00:04:24,479 before I finish talking about templates 132 00:04:24,479 --> 00:04:26,520 let's have a quick look at the main 133 00:04:26,520 --> 00:04:30,060 activity file there's quite a bit more 134 00:04:30,060 --> 00:04:31,500 code in this version 135 00:04:31,500 --> 00:04:33,390 than in the empty activity year template 136 00:04:33,390 --> 00:04:35,490 from the previous video but it's all to 137 00:04:35,490 --> 00:04:36,840 do with the setting up on the floating 138 00:04:36,840 --> 00:04:39,570 action button and the toolbar. Now there 139 00:04:39,570 --> 00:04:41,520 are two methods or functions if you're 140 00:04:41,520 --> 00:04:42,780 looking at this in the cotton course 141 00:04:42,780 --> 00:04:45,240 that weren't present before on create 142 00:04:45,240 --> 00:04:47,790 options menu in that on options item 143 00:04:47,790 --> 00:04:49,980 selected now they used to set up the 144 00:04:49,980 --> 00:04:52,610 menu and detect which menu items tapped 145 00:04:52,610 --> 00:04:55,710 also there's code in oncreate to cause 146 00:04:55,710 --> 00:04:57,390 the toolbar to be drawn and to allow 147 00:04:57,390 --> 00:04:59,520 clicks on the floating action button to 148 00:04:59,520 --> 00:05:02,250 be detected now if I don't want all that 149 00:05:02,250 --> 00:05:04,710 I can just delete it. Now in the oncreate 150 00:05:04,710 --> 00:05:07,440 method I'm going to go into there I'm 151 00:05:07,440 --> 00:05:08,940 going to actually delete all but the 152 00:05:08,940 --> 00:05:11,790 first two lines so everything from here 153 00:05:11,790 --> 00:05:14,070 down to the end of the right curly brace 154 00:05:14,070 --> 00:05:18,660 there I'm going to delete look so and 155 00:05:18,660 --> 00:05:21,300 I'm going to delete entirely on create 156 00:05:21,300 --> 00:05:23,820 options menu function and also the own 157 00:05:23,820 --> 00:05:29,820 options items selected so once I do that 158 00:05:29,820 --> 00:05:32,220 we basically end up with the same code 159 00:05:32,220 --> 00:05:34,169 as the previous template would have 160 00:05:34,169 --> 00:05:35,850 given us and by the way you can also 161 00:05:35,850 --> 00:05:37,740 delete activity underscore main I can 162 00:05:37,740 --> 00:05:40,440 come over here right-click and select 163 00:05:40,440 --> 00:05:43,620 delete I can go ahead and ignore those 164 00:05:43,620 --> 00:05:47,070 warnings click on delete anyway and 165 00:05:47,070 --> 00:05:48,840 that's removed activity and let's call 166 00:05:48,840 --> 00:05:51,540 main from this project and then what I 167 00:05:51,540 --> 00:05:53,250 could do is rename content underscore 168 00:05:53,250 --> 00:05:55,410 main to activity in this core main I 169 00:05:55,410 --> 00:05:56,669 come over here and right click that 170 00:05:56,669 --> 00:06:00,320 click on refactor and select rename I 171 00:06:00,320 --> 00:06:05,340 change that to activity now let's call 172 00:06:05,340 --> 00:06:09,570 my XML and click on refactor, now 173 00:06:09,570 --> 00:06:11,280 sometimes you might get an error doing 174 00:06:11,280 --> 00:06:13,979 that on a Mac or a Windows machine if 175 00:06:13,979 --> 00:06:15,960 that happens just right-click and choose copy, 176 00:06:15,960 --> 00:06:18,870 so if the rename didn't go through just 177 00:06:18,870 --> 00:06:20,970 right-click and select copy but the 178 00:06:20,970 --> 00:06:22,860 content owners called main.xml file and 179 00:06:22,860 --> 00:06:24,930 select copy and paste and then choose 180 00:06:24,930 --> 00:06:26,850 activity underscore - the file name and 181 00:06:26,850 --> 00:06:28,470 then you'll be able to delete content 182 00:06:28,470 --> 00:06:29,910 notice called main but in this case you 183 00:06:29,910 --> 00:06:33,180 can see they leave renamed worked ok, so 184 00:06:33,180 --> 00:06:34,320 now that I've done that notice over here 185 00:06:34,320 --> 00:06:37,229 there's this exclamation mark and if you 186 00:06:37,229 --> 00:06:39,660 click on that to show it we've got an 187 00:06:39,660 --> 00:06:42,180 arrow down here I can scroll it up to 188 00:06:42,180 --> 00:06:44,250 have a look at it it's just basically a 189 00:06:44,250 --> 00:06:45,150 warning about the 190 00:06:45,150 --> 00:06:47,070 that the surrounding layout did not 191 00:06:47,070 --> 00:06:49,290 actually include this layout, basically 192 00:06:49,290 --> 00:06:50,730 it's saying it's no longer shown inside 193 00:06:50,730 --> 00:06:52,950 another layout but we don't need to 194 00:06:52,950 --> 00:06:54,270 worry about that at this stage and what 195 00:06:54,270 --> 00:06:55,560 we can do instead is come over here to 196 00:06:55,560 --> 00:06:57,510 the text tab and by clicking on that we 197 00:06:57,510 --> 00:06:59,550 can see the xml for the layout. Now you 198 00:06:59,550 --> 00:07:01,320 don't need to worry about this xml very 199 00:07:01,320 --> 00:07:02,880 often well though it's very helpful if 200 00:07:02,880 --> 00:07:04,530 you select it and paste it all into your 201 00:07:04,530 --> 00:07:06,450 question if you have a problem with a 202 00:07:06,450 --> 00:07:09,420 layout not working what we can do to 203 00:07:09,420 --> 00:07:10,920 remove the error is delete the tools 204 00:07:10,920 --> 00:07:15,120 show in line, this line up here which is 205 00:07:15,120 --> 00:07:17,040 what Android studio is essentially is 206 00:07:17,040 --> 00:07:18,780 suggesting that we actually do so I'm 207 00:07:18,780 --> 00:07:20,130 gonna go ahead and do that delete it 208 00:07:20,130 --> 00:07:21,840 making sure that I'll leave the last 209 00:07:21,840 --> 00:07:24,270 greater than sign there and put that 210 00:07:24,270 --> 00:07:26,280 back in like so if we go back and click 211 00:07:26,280 --> 00:07:28,650 on the design tab now notice if the 212 00:07:28,650 --> 00:07:30,360 error is not valid anymore well we haven't 213 00:07:30,360 --> 00:07:32,820 got a red exclamation mark anymore so we 214 00:07:32,820 --> 00:07:34,500 created a project using the basic 215 00:07:34,500 --> 00:07:36,930 activity template but then we changed it 216 00:07:36,930 --> 00:07:38,340 to be exactly the same as the one 217 00:07:38,340 --> 00:07:41,010 created with the empty activity template 218 00:07:41,010 --> 00:07:43,080 now I don't expect you to remember all 219 00:07:43,080 --> 00:07:45,630 of that nor be able to do it yourself 220 00:07:45,630 --> 00:07:47,790 just yet but by the time you finish the 221 00:07:47,790 --> 00:07:49,920 course all of what we've done will make sense. 222 00:07:49,920 --> 00:07:52,140 So that's templates, they're just 223 00:07:52,140 --> 00:07:54,720 templates generating common code to save 224 00:07:54,720 --> 00:07:56,280 you having to type it but there's 225 00:07:56,280 --> 00:07:58,200 nothing magical about them you're 226 00:07:58,200 --> 00:08:00,570 completely free to make any change you 227 00:08:00,570 --> 00:08:02,520 want to once Android studio is created 228 00:08:02,520 --> 00:08:03,870 the project from the template. 229 00:08:03,870 --> 00:08:05,880 Now if Android studio changes in the 230 00:08:05,880 --> 00:08:07,830 future and temperance were these exact 231 00:08:07,830 --> 00:08:09,210 names are no longer available, 232 00:08:09,210 --> 00:08:10,830 you just want to choose ones that look 233 00:08:10,830 --> 00:08:12,780 similar and you can always delete any 234 00:08:12,780 --> 00:08:14,280 code that doesn't appear in the videos 235 00:08:14,280 --> 00:08:16,470 or add any code that I have in the video 236 00:08:16,470 --> 00:08:18,710 but which is missing from the template. 237 00:08:18,710 --> 00:08:21,030 Alright so let's move on to the next 238 00:08:21,030 --> 00:08:22,590 video where we're going to start coding 239 00:08:22,590 --> 00:08:27,020 up our first app. See you in the next video.