1 00:00:00,000 --> 00:00:02,610 [Seminar] [Android Apps (Now with Jelly Beans!)] 2 00:00:02,610 --> 00:00:04,210 [Jordan Jozwiak] [Harvard University] 3 00:00:04,210 --> 00:00:06,780 [This is CS50.] [CS50.TV] 4 00:00:06,780 --> 00:00:11,600 >> Hello everyone, and welcome to the Android Apps (Now with Jelly Beans!) Seminar. 5 00:00:11,600 --> 00:00:14,530 It's called Now with Jelly Beans, of course, because 6 00:00:14,530 --> 00:00:17,310 the latest version of the OS is called Jelly Bean. 7 00:00:17,310 --> 00:00:22,950 When I talked about Android last year and did this basically same seminar 8 00:00:22,950 --> 00:00:29,150 I was presenting the design patterns and stylistic choices for Gingerbread, 9 00:00:29,150 --> 00:00:31,250 even though Ice Cream Sandwich had come out at the time, 10 00:00:31,250 --> 00:00:33,680 and this is something we'll talk about going forward a little bit. 11 00:00:33,680 --> 00:00:35,720 Android, of course, has some issues with fragmentation, 12 00:00:35,720 --> 00:00:40,240 so the latest Google operating system is usually 13 00:00:40,240 --> 00:00:44,710 not really going to become common among the users for potentially up to a year, unfortunately. 14 00:00:44,710 --> 00:00:49,960 >> But disregarding that, we'll talk about plenty of reasons why 15 00:00:49,960 --> 00:00:53,000 Android is much better than iOS. 16 00:00:53,000 --> 00:00:56,410 Okay, we'll talk about Android versus iOS, 17 00:00:56,410 --> 00:00:59,770 some design paradigms, how you'll get set up in just one slide, 18 00:00:59,770 --> 00:01:03,410 and the big difference between this and last year is I just want to 19 00:01:03,410 --> 00:01:06,860 zoom through some conceptual things working with 20 00:01:06,860 --> 00:01:09,950 the operating system and then go into a really basic example. 21 00:01:09,950 --> 00:01:13,730 In fact, just the example that Google describes for building your first app, 22 00:01:13,730 --> 00:01:17,070 and we'll go through it together and just describe. 23 00:01:17,070 --> 00:01:22,100 >> I know many of you have only coded in C, so Java is already going to be 24 00:01:22,100 --> 00:01:27,220 a different perspective, and this way you'll get your feet wet 25 00:01:27,220 --> 00:01:31,540 and feel a little more comfortable with it hopefully. 26 00:01:31,540 --> 00:01:35,330 A quick summary, Android versus iOS. 27 00:01:35,330 --> 00:01:39,200 In Android there's this one-time developer fee of $25, 28 00:01:39,200 --> 00:01:42,390 none of this $100 per year thing. 29 00:01:42,390 --> 00:01:45,440 There are no restrictions, which means that if I want to submit an app, 30 00:01:45,440 --> 00:01:51,560 I just submit it, and there's no approval process like there is for iOS. 31 00:01:51,560 --> 00:01:53,890 You get to develop with Java using Eclipse, 32 00:01:53,890 --> 00:01:57,090 which is cross platform, so it doesn't matter if you have a Mac, 33 00:01:57,090 --> 00:01:59,600 Windows, or some Linux machine. 34 00:01:59,600 --> 00:02:03,730 Of course, with iOS you have to use XCode on a Mac, 35 00:02:03,730 --> 00:02:06,930 and Android is used by many more people, 36 00:02:06,930 --> 00:02:11,580 so if you want your app to have some sort of visibility or perhaps 37 00:02:11,580 --> 00:02:14,110 make a little money from advertisements 38 00:02:14,110 --> 00:02:17,450 you're probably more likely to benefit from Android. 39 00:02:17,450 --> 00:02:20,630 >> And one thing that's always been known about Android 40 00:02:20,630 --> 00:02:25,100 is the extreme amount of customization, whereas iOS is very fixed in their ways 41 00:02:25,100 --> 00:02:29,380 so that they can maintain security and control over every little aspect 42 00:02:29,380 --> 00:02:34,180 of the design within their OS and the individual apps. 43 00:02:34,180 --> 00:02:40,180 Android is very free form, and there's so many more possibilities. 44 00:02:40,180 --> 00:02:42,530 Okay, some design paradigms. 45 00:02:42,530 --> 00:02:49,600 Notice here just like iOS has tablets and iPhones and iPod Touches 46 00:02:49,600 --> 00:02:53,840 they have to support, say, like a dozen devices if you want to support 47 00:02:53,840 --> 00:02:55,450 historical things as well. 48 00:02:55,450 --> 00:02:58,410 With Android you have to support hundreds of devices, 49 00:02:58,410 --> 00:03:01,240 so there are some things that Google has been trying to implement 50 00:03:01,240 --> 00:03:04,970 to make the user experience a little more consistent, 51 00:03:04,970 --> 00:03:08,560 but we'll also talk about how this means that there are issues of fragmentation 52 00:03:08,560 --> 00:03:11,700 that you constantly have to be aware of supporting. 53 00:03:11,700 --> 00:03:16,140 Fragmentation means that there are many different operating systems, 54 00:03:16,140 --> 00:03:20,330 many different phones with different hardware running Android all at the same time, 55 00:03:20,330 --> 00:03:29,680 so this graph here goes back for about 6 months. 56 00:03:29,680 --> 00:03:36,460 Yeah, 6 months, and you can see how the darkest band 57 00:03:36,460 --> 00:03:39,330 on the bottom is Jelly Bean, which came out over the summer, 58 00:03:39,330 --> 00:03:42,970 and you can see right now probably about 2% of the phones have it. 59 00:03:42,970 --> 00:03:45,520 >> Ice Cream Sandwich came out over a year ago, 60 00:03:45,520 --> 00:03:47,770 and a quarter of the phones have it. 61 00:03:47,770 --> 00:03:53,910 There are still a lot of phones that are using OS version from years ago. 62 00:03:53,910 --> 00:03:57,820 One thing that Google has tried to do to make the user experience 63 00:03:57,820 --> 00:04:01,400 a lot more consistent across apps is starting with Honeycomb, 64 00:04:01,400 --> 00:04:04,320 which is the tablet version, then Ice Cream Sandwich and Jelly Bean, 65 00:04:04,320 --> 00:04:06,750 they're really trying to emphasize use of this action bar, 66 00:04:06,750 --> 00:04:10,430 which is what you can see at the top here, 67 00:04:10,430 --> 00:04:14,730 everything right here, including this little settings button. 68 00:04:14,730 --> 00:04:16,079 It's the design choice. 69 00:04:16,079 --> 00:04:18,000 Now, whenever you see the 3 dots it means click on this, 70 00:04:18,000 --> 00:04:21,880 and there will be a drop-down menu, which has some sort of settings. 71 00:04:21,880 --> 00:04:28,900 This only shows up because I clicked on this button. 72 00:04:28,900 --> 00:04:32,040 This is something that they're trying to use to make apps a little more consistent 73 00:04:32,040 --> 00:04:39,210 across the board and so the user has a better sense of uniform experience. 74 00:04:39,210 --> 00:04:42,640 >> Okay, so in case you haven't done this already or if you're watching at home 75 00:04:42,640 --> 00:04:50,210 then you can follow these steps to set up your coding environment. 76 00:04:50,210 --> 00:04:52,630 It will involve going to the website here. 77 00:04:52,630 --> 00:04:56,260 You can Google installing Android SDK 78 00:04:56,260 --> 00:04:59,130 and follow some descriptions there. 79 00:04:59,130 --> 00:05:01,310 They tell you everything. 80 00:05:01,310 --> 00:05:05,160 They have you install the SDK, download Eclipse Classic version, 81 00:05:05,160 --> 00:05:07,450 even though, of course, we're coding in Java. 82 00:05:07,450 --> 00:05:12,190 They recommend this one, and then this ADT, the Android Development Tools, 83 00:05:12,190 --> 00:05:17,300 is a plugin that you install into Eclipse which allows you to 84 00:05:17,300 --> 00:05:20,550 have a much more streamlined experience with Android within Eclipse. 85 00:05:20,550 --> 00:05:24,070 It will give you some graphical user interfaces 86 00:05:24,070 --> 00:05:29,610 for designing the views, and it will give you much more support 87 00:05:29,610 --> 00:05:34,840 and the ability to launch with the emulator and all that sort of thing. 88 00:05:34,840 --> 00:05:36,090 Application framework. 89 00:05:36,090 --> 00:05:41,590 >> I want to talk about how the projects within Android are structured, 90 00:05:41,590 --> 00:05:44,830 and then we'll go into a quick example, and hopefully 91 00:05:44,830 --> 00:05:48,820 you can ask questions at any point and feel comfortable by the end of it. 92 00:05:48,820 --> 00:05:50,090 We have some jargon first. 93 00:05:50,090 --> 00:05:55,330 An activity is like the screen you see when you launch an app. 94 00:05:55,330 --> 00:05:57,840 The entire experience is the activity itself, 95 00:05:57,840 --> 00:06:00,790 so you could think of it as a screen or a view, 96 00:06:00,790 --> 00:06:04,540 but it's actually the source code side of it. 97 00:06:04,540 --> 00:06:12,680 We still call the actual interface part of it a view. 98 00:06:12,680 --> 00:06:17,230 It's sort of like how in pset 7 we had the controller and then the view. 99 00:06:17,230 --> 00:06:20,690 The activity is more or less the controller, 100 00:06:20,690 --> 00:06:23,360 and the view is what we're doing in terms of the XML 101 00:06:23,360 --> 00:06:25,260 that is associated with that activity. 102 00:06:25,260 --> 00:06:29,340 Fragments are something that's implemented in Ice Cream Sandwich going forward. 103 00:06:29,340 --> 00:06:36,380 It's just an idea of taking parts of the view or the activity 104 00:06:36,380 --> 00:06:39,990 and dividing them into separate areas so that we can 105 00:06:39,990 --> 00:06:45,030 make our code more usable for these many different devices 106 00:06:45,030 --> 00:06:51,890 that we have to support, and it's just a way of trying to 107 00:06:51,890 --> 00:06:54,290 also factor out more code. 108 00:06:54,290 --> 00:07:01,090 Services are background things like playing music or navigation directions potentially. 109 00:07:01,090 --> 00:07:05,640 Content providers, these are APIs within the operating system 110 00:07:05,640 --> 00:07:09,070 that allow you to access shared data such as contact information, 111 00:07:09,070 --> 00:07:12,790 and then an intent is something we'll use when we want to launch 112 00:07:12,790 --> 00:07:17,210 a new activity from our current activity. 113 00:07:17,210 --> 00:07:20,980 >> Hello Android, this is going to be a project that we'll talk about. 114 00:07:20,980 --> 00:07:25,410 I'll actually do it for you, and then we'll do a thing where we click a button 115 00:07:25,410 --> 00:07:27,650 with some text and launch a new activity. 116 00:07:27,650 --> 00:07:30,110 Very straightforward, but I want to go through 117 00:07:30,110 --> 00:07:34,480 what will go into a project like this. 118 00:07:34,480 --> 00:07:39,260 When you make the new project you'll see within Eclipse 119 00:07:39,260 --> 00:07:41,900 there's this project called Hello Android. 120 00:07:41,900 --> 00:07:43,470 Then there are a whole bunch of folders here. 121 00:07:43,470 --> 00:07:46,760 The ones you're really concerned with are the source folder, 122 00:07:46,760 --> 00:07:54,540 the resource folder, the res, and to some extent the libs folder for libraries. 123 00:07:54,540 --> 00:08:00,070 And just so we're on the same page with that 124 00:08:00,070 --> 00:08:03,240 if you were to create a new project, 125 00:08:03,240 --> 00:08:08,270 you can do this right here, so if I want to call it HelloAndroid, 126 00:08:08,270 --> 00:08:11,100 and then it automatically does a whole bunch of the information for us. 127 00:08:11,100 --> 00:08:14,110 The package name is supposed to be a unique identifier 128 00:08:14,110 --> 00:08:16,710 that would normally be associated with the website, if you had one. 129 00:08:16,710 --> 00:08:24,430 >> For example, I will call this net.cs50.helloandroid. 130 00:08:24,430 --> 00:08:30,070 And I already have made this project once. 131 00:08:30,070 --> 00:08:40,059 Okay, sorry. Let's call it HelloCS50. 132 00:08:40,059 --> 00:08:42,929 And then it will ask things like what sort of icon do we want to make. 133 00:08:42,929 --> 00:08:48,840 This is all because of the Android Developer Tools interface, 134 00:08:48,840 --> 00:08:53,350 the plugin that we added to Eclipse, so we can choose things like 135 00:08:53,350 --> 00:08:58,320 what icon we want, and that would go into that depth if we want. 136 00:08:58,320 --> 00:09:04,360 But in the end, just make the project, 137 00:09:04,360 --> 00:09:07,040 and everything shows up right here. 138 00:09:07,040 --> 00:09:10,030 As I showed on the other screen, 139 00:09:10,030 --> 00:09:14,070 we have the source folder, and I'll talk about what all this means soon, 140 00:09:14,070 --> 00:09:19,090 but you can see that immediately it takes us to the HelloCS50 screen. 141 00:09:19,090 --> 00:09:24,690 There are some buttons over here for adding text and text field layouts. 142 00:09:24,690 --> 00:09:28,910 It's sort of like XCode but unfortunately not quite as streamlined. 143 00:09:28,910 --> 00:09:37,790 But the clips will be your friend and ADT in particular for doing this. 144 00:09:37,790 --> 00:09:40,920 >> Okay, the source folder is where 145 00:09:40,920 --> 00:09:43,820 the Java files are held, and this is actually the launch of your program. 146 00:09:43,820 --> 00:09:45,990 It's basically like the controllers. 147 00:09:45,990 --> 00:09:49,020 The resource folder or the res is where we'll keep screen layouts, 148 00:09:49,020 --> 00:09:54,110 images, sounds, text, animation and everything else, libs, obviously, libraries. 149 00:09:54,110 --> 00:10:01,700 And the Manifest, it's the way that the Android system knows 150 00:10:01,700 --> 00:10:03,130 how to interact with your app. 151 00:10:03,130 --> 00:10:07,980 It tells it about permissions and what activities will actually be run. 152 00:10:07,980 --> 00:10:12,350 It's all the essential information that's necessary to run your app. 153 00:10:12,350 --> 00:10:17,990 The source folder is—if we expand it as you can see right here. 154 00:10:17,990 --> 00:10:23,920 And an example that a code would look—this is the default code. 155 00:10:23,920 --> 00:10:27,930 As we called it, MainActivity. 156 00:10:27,930 --> 00:10:33,230 By default, we get onCreate, which is a function that's called when this activity loads, 157 00:10:33,230 --> 00:10:37,150 so you want to call onCreate (savedInstanceState). 158 00:10:37,150 --> 00:10:40,440 This will restore any bits of information 159 00:10:40,440 --> 00:10:44,910 in case the app was paused. 160 00:10:44,910 --> 00:10:48,790 >> There's a whole activity life cycle chart which you can look at 161 00:10:48,790 --> 00:10:50,380 when you get a little more in-depth in it, 162 00:10:50,380 --> 00:10:56,210 but the most important line of code there is this setContentView function 163 00:10:56,210 --> 00:11:00,290 which will load the resource file for the layout. 164 00:11:00,290 --> 00:11:03,590 Then what onCreate here is basically doing is it's saying 165 00:11:03,590 --> 00:11:07,010 when I create this activity I want to load this layout. 166 00:11:07,010 --> 00:11:12,030 And we'll talk about what that layout is in a second. 167 00:11:12,030 --> 00:11:16,230 If we were to launch that here it looks exactly like this, 168 00:11:16,230 --> 00:11:22,880 and by default because we have Eclipse doing this and ADT doing it for us 169 00:11:22,880 --> 00:11:28,210 this is all boilerplate code that's created for us, 170 00:11:28,210 --> 00:11:34,630 and we can move on and change things as we like. 171 00:11:34,630 --> 00:11:38,190 The resource folder, like I said, this is where we hold all layouts, 172 00:11:38,190 --> 00:11:41,150 drawables in terms of any sort of images— 173 00:11:41,150 --> 00:11:46,360 they're called drawables—and then sound files, menus. 174 00:11:46,360 --> 00:11:51,170 It's a way of a whole bunch of XML in a nice sorted fashion. 175 00:11:51,170 --> 00:11:58,030 As you can see, there are .hdpi, ldpi, mdpi and xhdpi folders for the drawables. 176 00:11:58,030 --> 00:12:02,220 These are different screen densities, so high density pixel per inch, 177 00:12:02,220 --> 00:12:05,710 low density, medium density, extra high density. 178 00:12:05,710 --> 00:12:08,900 >> This is, again, so we can support all these different devices, 179 00:12:08,900 --> 00:12:15,100 and ideally, you will have different images for each different resolution. 180 00:12:15,100 --> 00:12:20,740 Layouts are where we include all the different layouts that will be used in different activities. 181 00:12:20,740 --> 00:12:24,150 They could also include layouts for fragments, 182 00:12:24,150 --> 00:12:26,670 which means partial activities basically. 183 00:12:26,670 --> 00:12:29,610 If you want to do something cool like whenever you rotate the phone 184 00:12:29,610 --> 00:12:33,900 you want it to have a different layout you can do layout-landscape as a different folder, 185 00:12:33,900 --> 00:12:39,000 and Android automatically has a whole bunch of these rules within the resource folder, 186 00:12:39,000 --> 00:12:44,690 so it will know to look at different folders based on the current setup of the phone, 187 00:12:44,690 --> 00:12:46,740 what sort of hardware you're using. 188 00:12:46,740 --> 00:12:52,350 Similarly, this values-v11 here and values-v14 189 00:12:52,350 --> 00:12:55,460 and the default values folder this is for— 190 00:12:55,460 --> 00:13:01,730 well, values by itself, this will be the default values across all versions of the operating system. 191 00:13:01,730 --> 00:13:06,390 On the other hand, v11 and v14 correspond with Honeycomb 192 00:13:06,390 --> 00:13:08,780 and Ice Cream Sandwich, respectively. 193 00:13:08,780 --> 00:13:12,800 Again, these are ways that—within these folders are different strings 194 00:13:12,800 --> 00:13:17,550 and styles, so you can customize the appearance of your app 195 00:13:17,550 --> 00:13:22,660 or potentially even its functionality, although that would probably be a bad idea, 196 00:13:22,660 --> 00:13:25,890 based on the version of the operating system. 197 00:13:25,890 --> 00:13:28,760 >> And I talked about all this here. 198 00:13:28,760 --> 00:13:35,870 Again, drawable, raw, layout, values. 199 00:13:35,870 --> 00:13:41,380 The defaults, if we go into Hello Android or Hello CS50 right now 200 00:13:41,380 --> 00:13:43,310 it will look just like this. 201 00:13:43,310 --> 00:13:45,800 It will say hello world. 202 00:13:45,800 --> 00:13:58,430 As you can see right here with the Android text 203 00:13:58,430 --> 00:14:04,420 the text for this text view, which is basically just a label, 204 00:14:04,420 --> 00:14:06,900 is what we would call it in iOS. 205 00:14:06,900 --> 00:14:11,580 It has this funny notation @string/hello_world. 206 00:14:11,580 --> 00:14:16,200 This is so we can—we attempt to extract all of the strings, 207 00:14:16,200 --> 00:14:19,740 all of the hard-coded strings in Android into a separate folder, 208 00:14:19,740 --> 00:14:23,610 which happens to be in values.strings. 209 00:14:23,610 --> 00:14:29,300 If we look here, we can see that we have a hard-coded string saved here 210 00:14:29,300 --> 00:14:33,910 for hello world, and it's called hello world, and this is sort of a 211 00:14:33,910 --> 00:14:39,230 common design decision across many platforms that you want to— 212 00:14:39,230 --> 00:14:41,780 strings are the sort of things that we want to potentially be able to change. 213 00:14:41,780 --> 00:14:48,050 We don't want to control F in our source code file in our XML. 214 00:14:48,050 --> 00:14:50,760 We want to be able to change this whenever we get the chance. 215 00:14:50,760 --> 00:14:53,640 >> This XML file, strings.xml, is a way of extracting 216 00:14:53,640 --> 00:14:56,710 hard-coded strings that would otherwise be in our layout resources 217 00:14:56,710 --> 00:15:03,530 or in our source code. 218 00:15:03,530 --> 00:15:08,190 The Manifest is where we keep this essential information about the application. 219 00:15:08,190 --> 00:15:11,050 It includes the package name. That has to be a unique identifier. 220 00:15:11,050 --> 00:15:16,020 Unlike the App Store for iOS the names of the applications 221 00:15:16,020 --> 00:15:19,980 don't have to be unique, and you can in fact change the name of your application 222 00:15:19,980 --> 00:15:23,570 after you submit it, so if I start out with Angry Birds, 223 00:15:23,570 --> 00:15:26,840 and then I make an update, and I don't want to release a new version 224 00:15:26,840 --> 00:15:34,230 I guess I could call it Angry Birds 2 while I'm on the same release cycle. 225 00:15:34,230 --> 00:15:36,780 It would just show up as an update. 226 00:15:36,780 --> 00:15:39,790 The only thing that has to be unique is this package name, 227 00:15:39,790 --> 00:15:41,280 which not many people will see. 228 00:15:41,280 --> 00:15:44,680 Well, you'd only see if you were looking at the source code or if there is some sort of error. 229 00:15:44,680 --> 00:15:47,580 The components include activities, so we have to declare 230 00:15:47,580 --> 00:15:49,070 any sort of activity we use. 231 00:15:49,070 --> 00:15:53,430 This is a permission sort of thing. 232 00:15:53,460 --> 00:15:58,150 >> Because Android doesn't have this rigorous application process 233 00:15:58,150 --> 00:16:02,210 for submitting apps instead they just do everything based off of permissions 234 00:16:02,210 --> 00:16:04,300 and declaring things in the Manifest. 235 00:16:04,300 --> 00:16:06,380 The SDK version is also important. 236 00:16:06,380 --> 00:16:10,330 We can set use as SDK minimum version something. 237 00:16:10,330 --> 00:16:16,010 Obviously, you want to create code 238 00:16:16,010 --> 00:16:18,570 and design for the newer operating systems. 239 00:16:18,570 --> 00:16:20,840 Perhaps not the newest, maybe not Jelly Bean. 240 00:16:20,840 --> 00:16:23,610 Maybe you want to design primarily for Ice Cream Sandwich, 241 00:16:23,610 --> 00:16:27,120 although they are pretty much the same in terms of design decisions 242 00:16:27,120 --> 00:16:30,550 and the actual libraries and APIs. 243 00:16:30,550 --> 00:16:36,250 But rather than have people 244 00:16:36,250 --> 00:16:40,050 of the original Android version—like Donut way back when— 245 00:16:40,050 --> 00:16:43,420 who still happen to be using that try to download your application and then say 246 00:16:43,420 --> 00:16:46,160 it doesn't work on my phone you can set the minimum version 247 00:16:46,160 --> 00:16:48,620 and have it be Gingerbread or Ice Cream Sandwich 248 00:16:48,620 --> 00:16:53,300 or whatever you feel comfortable submitting. 249 00:16:53,300 --> 00:16:57,170 And this is an example of the Manifest. We can look at the actual one in a bit. 250 00:16:57,170 --> 00:17:00,900 >> Okay, we'll just go forward with the demo now. 251 00:17:00,900 --> 00:17:08,900 This is the demo if you Google Android build your first app or something like that. 252 00:17:08,900 --> 00:17:12,089 We'll go through everything that's going on in there, 253 00:17:12,089 --> 00:17:17,690 and sorry, just for a reference who here knows Java to any extent? 254 00:17:17,690 --> 00:17:28,310 Okay, so Java is new coding language for most people coming out of CS50. 255 00:17:28,310 --> 00:17:31,850 Briefly, to go over again what it says here, 256 00:17:31,850 --> 00:17:35,580 what we want to do is rather than adjust this hello world application 257 00:17:35,580 --> 00:17:38,620 that all it does is it launches and it says hello world 258 00:17:38,620 --> 00:17:43,580 and it says hello CS50 at the top because that's the activity name 259 00:17:43,580 --> 00:17:47,190 we're going to attempt to make a text field and a button 260 00:17:47,190 --> 00:17:50,680 so that when we press this button it will take the text 261 00:17:50,680 --> 00:17:53,660 from the text field and launch a new activity, and it will say 262 00:17:53,660 --> 00:17:58,380 whatever that text said in the activity, and this is relatively simple. 263 00:17:58,380 --> 00:18:00,650 It's not very interesting. You wouldn't want to release any app like this. 264 00:18:00,650 --> 00:18:03,880 But it demonstrates some important design things. 265 00:18:03,880 --> 00:18:07,030 >> We'll have to interact with the Manifest, with the layout files, 266 00:18:07,030 --> 00:18:12,440 with the source code, and you can see how to launch one activity from another. 267 00:18:19,740 --> 00:18:23,060 We'll start with the layout for our first activity, 268 00:18:23,060 --> 00:18:27,320 which I know it's kind of small, 269 00:18:27,320 --> 00:18:32,660 but as you can see right now, all it is is it has our logo that we created, 270 00:18:32,660 --> 00:18:36,310 the activity name, and it says hello world in the center. 271 00:18:36,310 --> 00:18:40,400 Instead, I'm first going to make this into a linear layout, 272 00:18:40,400 --> 00:18:42,350 a relative layout, and these are all things 273 00:18:42,350 --> 00:18:45,870 you could look up and are worth going over at some point in terms of the layouts. 274 00:18:45,870 --> 00:18:47,310 It's a lot like HTML. 275 00:18:47,310 --> 00:18:52,640 We said it's not really worth knowing in terms of a programmatic sense, 276 00:18:52,640 --> 00:18:55,840 but there are so many different layouts you can control, 277 00:18:55,840 --> 00:18:57,970 and it's just like CSS in terms of styles. 278 00:18:57,970 --> 00:19:01,590 >> There's a lot going on just in terms of the aesthetics here, 279 00:19:01,590 --> 00:19:08,930 and yeah, it's worth learning, but it's the sort of thing you should look up 280 00:19:08,930 --> 00:19:11,810 as you go and you feel like you need something new. 281 00:19:11,810 --> 00:19:17,030 I can take advantage of some of the ADT tools here at least to start off with. 282 00:19:17,030 --> 00:19:21,930 I'll make a text field, and I will make a button. 283 00:19:21,930 --> 00:19:24,100 Put them right next to each other. 284 00:19:24,100 --> 00:19:28,200 They already resize appropriately. 285 00:19:28,200 --> 00:19:31,320 Sorry, again, coming back to the difference between linear layout and relative layout, 286 00:19:31,320 --> 00:19:34,830 a linear layout gives a little more functionality 287 00:19:34,830 --> 00:19:38,720 in terms of filling space and making sure 288 00:19:38,720 --> 00:19:42,790 we position things horizontally or vertically. 289 00:19:42,790 --> 00:19:46,130 The big difference between a linear layout and a relative layout 290 00:19:46,130 --> 00:19:50,250 is the relative layout positions everything relative to other views, 291 00:19:50,250 --> 00:19:52,630 and the good thing about it is that it takes less time to draw 292 00:19:52,630 --> 00:19:56,120 because the operating system looks at all the code and says this goes 293 00:19:56,120 --> 00:19:58,270 relative to this here, here. 294 00:19:58,270 --> 00:20:03,170 It means it makes fewer measurements than a whole bunch of nested linear layouts, 295 00:20:03,170 --> 00:20:06,840 which I would have to do if I wanted things to go vertically here. 296 00:20:06,840 --> 00:20:10,510 >> I already have some things going horizontally, so I would need a different linear layout 297 00:20:10,510 --> 00:20:14,560 to nest them vertically, but if I were doing a relative layout I could say 298 00:20:14,560 --> 00:20:18,140 keep this to the left, this to the right and these things below. 299 00:20:18,140 --> 00:20:21,850 But because I want them to fill right now I'm going to use a linear layout, 300 00:20:21,850 --> 00:20:24,510 and you can see it already does a lot of these things for us. 301 00:20:24,510 --> 00:20:28,890 All views need to have a property of width and height, 302 00:20:28,890 --> 00:20:33,410 and this happens to have a weight of 1, and this has a weight of 1 303 00:20:33,410 --> 00:20:36,050 so that it fills the entire space. 304 00:20:36,050 --> 00:20:38,460 But the width and height are most important. 305 00:20:38,460 --> 00:20:45,880 This ID field right here is not important within the actual layout 306 00:20:45,880 --> 00:20:49,060 unless we're using a relative layout and saying we can reference 307 00:20:49,060 --> 00:20:52,990 one view position in reference to another. 308 00:20:52,990 --> 00:20:56,170 If we had a relative layout we could say position this below this one 309 00:20:56,170 --> 00:20:59,390 or above this ID, but this ID will be important 310 00:20:59,390 --> 00:21:01,660 when we're using it in our code because we can reference 311 00:21:01,660 --> 00:21:05,610 these individual views in this way. 312 00:21:05,610 --> 00:21:10,350 >> And then you can see the tag requestFocus, which is something you probably saw in pset 7. 313 00:21:10,350 --> 00:21:15,370 There was an auto focus tag or something along those lines for one of the fields. 314 00:21:15,370 --> 00:21:22,100 And one thing, of course, we can do just like in HTML, 315 00:21:22,100 --> 00:21:25,660 and notice one of the nice things about ADT is that it autocompletes here. 316 00:21:25,660 --> 00:21:29,930 Maybe not as nicely as it did in XCode, but it does nonetheless, 317 00:21:29,930 --> 00:21:32,860 and we could say hint and then give it some sort of string 318 00:21:32,860 --> 00:21:37,070 like write a message, 319 00:21:37,070 --> 00:21:41,300 and if I give it a moment, also notice that I get a little error here 320 00:21:41,300 --> 00:21:44,610 saying that we should use a string resource for this, 321 00:21:44,610 --> 00:21:48,820 so if I let it do some of the hard work for me 322 00:21:48,820 --> 00:21:57,320 I can select this string, do refractor Android and extract Android string. 323 00:21:57,320 --> 00:22:00,570 I can give it that name, write a message, 324 00:22:00,570 --> 00:22:04,180 and what it did now is it replaced that hard-coded string 325 00:22:04,180 --> 00:22:07,980 with a reference to the string in the string's .xml file, 326 00:22:07,980 --> 00:22:10,200 which means now this is here. 327 00:22:10,200 --> 00:22:15,560 >> And I know that seems a little trivial and like extra work at this point, 328 00:22:15,560 --> 00:22:17,840 but when you have a whole bunch of things, a whole bunch of strings, 329 00:22:17,840 --> 00:22:20,750 that's really important, and particularly for localization 330 00:22:20,750 --> 00:22:26,920 because as I mentioned earlier, Android is a very globally used operating system 331 00:22:26,920 --> 00:22:35,950 that you can simply do values-en or values-sp 332 00:22:35,950 --> 00:22:44,320 or something for English or Spanish or other languages. 333 00:22:44,320 --> 00:22:50,210 And then you probably haven't had this experience yet 334 00:22:50,210 --> 00:22:53,030 since we didn't do much JavaScript in terms of web programming, 335 00:22:53,030 --> 00:22:56,850 but we can also do this field called onClick for the button, 336 00:22:56,850 --> 00:23:02,350 and this is a way of referencing within our layout 337 00:23:02,350 --> 00:23:06,570 that we want a certain function to be called in our source code. 338 00:23:06,570 --> 00:23:09,420 I'm going to call this sendMessage, 339 00:23:09,420 --> 00:23:13,390 and in order to make this work that means in the main activity here 340 00:23:13,390 --> 00:23:19,180 I will have to create a function that is public 341 00:23:19,180 --> 00:23:22,250 so that it can be accessed by the layout. 342 00:23:22,250 --> 00:23:25,930 It's void because you just don't want it to return anything. 343 00:23:25,930 --> 00:23:37,760 We'll call it sendMessage, and it takes a view, 344 00:23:37,760 --> 00:23:43,690 which is a sort of context that we can anchor this method 345 00:23:43,690 --> 00:23:47,160 to that individual view when we're running it. 346 00:23:47,160 --> 00:23:50,090 >> And I don't know if you noticed—well, you wouldn't have noticed, 347 00:23:50,090 --> 00:24:02,370 but at this point right here 348 00:24:02,370 --> 00:24:06,930 this view is a type that has not yet been included, 349 00:24:06,930 --> 00:24:10,630 and of course, you all know from CS50 about not including types. 350 00:24:10,630 --> 00:24:13,120 The nice thing about IDE's like Eclipse is that 351 00:24:13,120 --> 00:24:17,520 if you forget to include a type it will underline with a little red squiggly 352 00:24:17,520 --> 00:24:19,680 and then give you the option to import the view. 353 00:24:19,680 --> 00:24:23,130 A quick way of doing that is control shift O, and it will import 354 00:24:23,130 --> 00:24:27,070 all the files that it needs to. 355 00:24:27,070 --> 00:24:33,670 Now that we have this function sendMessage, 356 00:24:33,670 --> 00:24:46,560 which is going to send message from the MainActivity to a new activity, 357 00:24:46,560 --> 00:24:55,230 we're going to have to use an intent to launch the new activity from this one, 358 00:24:55,230 --> 00:25:02,240 so we'll probably want to create a global constant or also particularly a public constant 359 00:25:02,240 --> 00:25:06,240 that will help us know how we're going to reference the string 360 00:25:06,240 --> 00:25:09,090 because when we create an intent we're not just calling a new activity, 361 00:25:09,090 --> 00:25:11,520 but we can also give it a bundle of information. 362 00:25:11,520 --> 00:25:14,720 A bundle is actually the term that Android uses, 363 00:25:14,720 --> 00:25:21,470 and it's a way of saying give it extra data, which it can't be of a very sophisticated data type, 364 00:25:21,470 --> 00:25:24,510 but we can definitely include booleans or strings or ints. 365 00:25:24,510 --> 00:25:29,750 That sort of thing. 366 00:25:29,750 --> 00:25:34,230 >> Feel free to ask about any of these qualifiers here, 367 00:25:34,230 --> 00:25:36,900 public, final, static. 368 00:25:36,900 --> 00:25:38,610 Public is the important one. 369 00:25:38,610 --> 00:25:47,090 It means that we can access this variable from other source code files 370 00:25:47,090 --> 00:25:49,590 such as when we create a new activity. 371 00:25:49,590 --> 00:25:52,190 We can reference the string within this file. 372 00:25:52,190 --> 00:25:57,160 If it's private, it means it's limited to this individual file here. 373 00:25:57,160 --> 00:26:02,430 But we'll call this something like extra message. 374 00:26:16,260 --> 00:26:23,300 Now we have made this function, and if we click the button, this function will be called. 375 00:26:23,300 --> 00:26:27,970 But we haven't done anything, obviously, with the function. 376 00:26:27,970 --> 00:26:33,130 What we want to do now is create a new activity, 377 00:26:33,130 --> 00:26:35,350 the activity that will actually be launched. 378 00:26:35,350 --> 00:26:44,330 In Eclipse we can say new, Android activity, blank activity. 379 00:26:44,330 --> 00:26:46,530 We can give it a name. 380 00:26:46,530 --> 00:26:57,050 Let's call it our result activity. 381 00:26:57,050 --> 00:27:04,430 And then this hierarchical parent field is a way of specifying 382 00:27:04,430 --> 00:27:08,180 how the activities are related to each other. 383 00:27:08,180 --> 00:27:11,020 Because this one will be launched from the other activity 384 00:27:11,020 --> 00:27:27,520 we should specify that it has the parent main activity. 385 00:27:27,520 --> 00:27:31,220 >> And I could have clicked on the next field there, which would have told us 386 00:27:31,220 --> 00:27:33,830 some optional fields that it was also including, 387 00:27:33,830 --> 00:27:38,800 but the nice thing about ADT, again, is that if I scroll down here 388 00:27:38,800 --> 00:27:45,740 in the Manifest remember that all of this really was included by default 389 00:27:45,740 --> 00:27:48,930 when we originally created the project, 390 00:27:48,930 --> 00:27:53,090 and now because we used this create a new activity rather than just 391 00:27:53,090 --> 00:27:58,610 adding our own class file because we went through their GUI for doing this 392 00:27:58,610 --> 00:28:01,930 it already added all of this to the Manifest for us, 393 00:28:01,930 --> 00:28:06,020 which just means that now the operating system won't complain 394 00:28:06,020 --> 00:28:09,400 when we try to launch this activity. 395 00:28:09,400 --> 00:28:14,620 And it also, of course, gave it a label, which also shows up in strings. 396 00:28:14,620 --> 00:28:17,340 It does a lot of things on the back end for us. 397 00:28:17,340 --> 00:28:20,140 Anyway, now that we have this result activity as well, 398 00:28:20,140 --> 00:28:23,810 the actual source code file in the source folder, 399 00:28:23,810 --> 00:28:33,540 and you can see—to briefly touch on this— 400 00:28:33,540 --> 00:28:37,790 because we told it about this hierarchical parent 401 00:28:37,790 --> 00:28:46,640 it gave this field android.r.ide.home in this onOption selected thing. 402 00:28:46,640 --> 00:28:49,020 >> What this is basically saying is there's a menu button now 403 00:28:49,020 --> 00:28:53,110 that in the top left corner of the screen 404 00:28:53,110 --> 00:29:02,220 if I go back to the PowerPoint where we first talked about action bar 405 00:29:02,220 --> 00:29:05,380 because of the hierarchical parent now there's a little arrow back here 406 00:29:05,380 --> 00:29:10,610 and this is clickable so we should be able to—after we actually run this app— 407 00:29:10,610 --> 00:29:15,360 go back, and it's a way of saving us a little bit of code there. 408 00:29:26,220 --> 00:29:40,180 Now within this—well, it seems to be complaining about that for a moment. 409 00:29:40,180 --> 00:29:43,910 Let's comment that out. 410 00:29:43,910 --> 00:29:48,260 Now, in order to actually interact with these 2 activities, 411 00:29:48,260 --> 00:29:55,020 we have to create an intent which includes all of this data. 412 00:29:55,020 --> 00:29:58,950 In sendMessage now I'm going to cruise through some bits of code 413 00:29:58,950 --> 00:30:00,700 and explain that as I go. 414 00:30:00,700 --> 00:30:06,280 An intent, as I said, is a way to launch one activity from another. 415 00:30:06,280 --> 00:30:10,000 Intent is the type. We're creating a variable myIntent. 416 00:30:10,000 --> 00:30:13,980 >> And this is actually an object, which is why we have to call new. 417 00:30:13,980 --> 00:30:18,500 It's common Java usage there, which might take a little bit of getting used to. 418 00:30:18,500 --> 00:30:23,900 This means we're using the activity or the class MainActivity, 419 00:30:23,900 --> 00:30:28,630 and intent in that way takes the activity it's going from 420 00:30:28,630 --> 00:30:36,060 and the activity it's going to, which we call ResultActivity, 421 00:30:36,060 --> 00:30:38,170 and that is its own class. 422 00:30:38,170 --> 00:30:41,720 Include that. 423 00:30:41,720 --> 00:30:47,370 And then as I mentioned before, we gave that ID to the edit text field 424 00:30:47,370 --> 00:30:53,710 and the text view, so in order to programmatically capture 425 00:30:53,710 --> 00:31:01,860 the text that exists in the editText 426 00:31:01,860 --> 00:31:06,710 we use this function called findViewByID, 427 00:31:06,710 --> 00:31:13,660 which is similar to something that you'll use when trying to get fields from the DOM. 428 00:31:13,660 --> 00:31:17,560 Perhaps David talks about this in lecture. 429 00:31:17,560 --> 00:31:26,600 But it's just a way of getting those—that's why we tag these things with those fields. 430 00:31:26,600 --> 00:31:31,110 Id.editText1. 431 00:31:31,110 --> 00:31:35,220 It's already autocompleting for me there. 432 00:31:35,220 --> 00:31:42,010 >> And then we'll also include—this editText now that we have the field 433 00:31:42,010 --> 00:31:47,640 within our program, all that findViewByID did was basically 434 00:31:47,640 --> 00:31:50,870 you could think of it as give us a reference to that editText. 435 00:31:50,870 --> 00:31:54,570 Now we want to get the string or the message 436 00:31:54,570 --> 00:32:00,670 within that editText which we can do by using the— 437 00:32:00,670 --> 00:32:03,360 you could think of editText as a struct— 438 00:32:03,360 --> 00:32:07,600 we call it an object in Java—with a whole bunch of different methods 439 00:32:07,600 --> 00:32:10,650 or fields and attributes attached to it. 440 00:32:10,650 --> 00:32:15,510 When I say editText. it gives me the chance to access 441 00:32:15,510 --> 00:32:19,770 a method such as getText, which will get the current text 442 00:32:19,770 --> 00:32:23,990 in that editText and then convert that explicitly 443 00:32:23,990 --> 00:32:26,520 to a string, and we have saved the message. 444 00:32:26,520 --> 00:32:32,730 Now we actually want to do something with that intent we created just moments ago. 445 00:32:32,730 --> 00:32:40,250 I will put extra as it's called in the intent, 446 00:32:40,250 --> 00:32:43,330 which, again, like I said before, this is a way of creating the bundle 447 00:32:43,330 --> 00:32:45,980 with a whole bunch of data. 448 00:32:45,980 --> 00:32:50,920 Extra message is our tag so we know what we're saving this under, 449 00:32:50,920 --> 00:32:56,700 and then I'm including the message. 450 00:32:56,700 --> 00:33:04,870 >> And then we can actually start the activity by giving it the parameter myIntent. 451 00:33:04,870 --> 00:33:10,630 And this will actually launch the activity, so now since I upgraded to Windows 8 452 00:33:10,630 --> 00:33:15,870 there might be some errors that show up briefly, but try to ignore them. 453 00:33:28,010 --> 00:33:30,310 And this is the Android emulator. 454 00:33:30,310 --> 00:33:33,540 Hopefully the code will actually pop up in a moment. 455 00:33:33,540 --> 00:33:36,680 Again, ignoring all those little things. 456 00:33:36,680 --> 00:33:42,290 But the Android emulator you can emulate any version of the operating system 457 00:33:42,290 --> 00:33:45,180 from the earliest ones, Donut, Gingerbread. 458 00:33:45,180 --> 00:33:49,640 This is Jelly Bean 4.1. 459 00:33:49,640 --> 00:33:53,150 But as you can see right now, this is something we made. 460 00:33:53,150 --> 00:33:55,780 This has the MainActivity, the editText, the button. 461 00:33:55,780 --> 00:34:01,550 I can type something here, but what are we missing at this point? 462 00:34:01,550 --> 00:34:05,190 We made the field in the MainActivity that's going to send the message 463 00:34:05,190 --> 00:34:08,570 to the other activity, but at this point 464 00:34:08,570 --> 00:34:12,330 we haven't done anything in the other activity, so it's not going to receive anything. 465 00:34:12,330 --> 00:34:18,800 Let's finish up that part before we move on. 466 00:34:18,800 --> 00:34:25,760 >> This result activity, what we'll need to do is we'll need to do is we'll need to— 467 00:34:25,760 --> 00:34:30,070 we can also reference the intent within this activity, 468 00:34:30,070 --> 00:34:33,580 so we have to capture the intent, which is really simple. 469 00:34:33,580 --> 00:34:40,780 It's a function included in the activity class, 470 00:34:40,780 --> 00:34:47,610 so we can just say "Get the message from the intent." 471 00:34:47,610 --> 00:34:56,260 "Intent myIntent = getIntent." 472 00:34:56,260 --> 00:35:00,030 And this will give us—now myIntent is an object, 473 00:35:00,030 --> 00:35:05,380 which is a reference to the intent that led us to this activity. 474 00:35:05,380 --> 00:35:10,130 MainActivity calls and gives extra information to result activity. 475 00:35:10,130 --> 00:35:14,070 Result activity now looks at the intent that led it here, 476 00:35:14,070 --> 00:35:23,540 and we can access the message that we created in the other one, 477 00:35:23,540 --> 00:35:26,370 getStringExtra, and this will autocomplete for me. 478 00:35:26,370 --> 00:35:31,190 And then remember, the way we tagged that bit of data 479 00:35:31,190 --> 00:35:38,400 was by giving it this field right here, this public string, 480 00:35:38,400 --> 00:35:42,080 which is the tag for the string we actually included. 481 00:35:42,080 --> 00:35:48,450 >> If I say MainActivity.Extra_Message 482 00:35:48,450 --> 00:35:52,200 it's right there, and this is sort of like the parameter name 483 00:35:52,200 --> 00:35:55,330 when we passed code from the controller to the template, 484 00:35:55,330 --> 00:35:57,680 when we'd say title arrow something. 485 00:35:57,680 --> 00:36:01,140 It's the same sort of idea there. 486 00:36:01,140 --> 00:36:07,130 Now, remember this. 487 00:36:07,130 --> 00:36:12,230 We also had this auto created or auto generated code 488 00:36:12,230 --> 00:36:16,410 for result activity, which includes the text view in the middle 489 00:36:16,410 --> 00:36:19,690 that says hello world. 490 00:36:19,690 --> 00:36:27,510 We could potentially make that bigger. 491 00:36:27,510 --> 00:36:33,980 Let's do that first. 492 00:36:33,980 --> 00:36:39,550 We could make it 40 density independent pixels. 493 00:36:39,550 --> 00:36:42,670 Let's see, that's super huge. That will be okay. 494 00:36:42,670 --> 00:36:48,180 And then in order to reference this individual text view within our code 495 00:36:48,180 --> 00:36:53,890 we'll need to give it an ID, 496 00:36:53,890 --> 00:36:58,350 and in order to do that we say @+id, which means we assign it ID. 497 00:36:58,350 --> 00:37:02,480 If we just said @id it would assume we're referencing an ID that already exists, 498 00:37:02,480 --> 00:37:07,760 and let's call this our resultTextView. 499 00:37:07,760 --> 00:37:10,570 And notice that in XML there's no need for semicolons or anything. 500 00:37:10,570 --> 00:37:12,380 Everything is within these tags. 501 00:37:12,380 --> 00:37:15,530 It's very similar to HTML in a lot of senses. 502 00:37:15,530 --> 00:37:22,760 >> It takes a little bit of getting used to, but eventually you'll feel pretty comfortable with it. 503 00:37:22,760 --> 00:37:31,320 Let's update the text for our result text view, 504 00:37:31,320 --> 00:37:35,650 and as we did when we were getting that edit text field in our other activity 505 00:37:35,650 --> 00:37:46,630 in MainActivity here we will also likewise— 506 00:37:46,630 --> 00:37:54,890 we'll find the view by the ID. 507 00:37:54,890 --> 00:37:59,640 And R, in case I didn't mention this before, is the reference to our resources folder, 508 00:37:59,640 --> 00:38:05,350 and then ID is a reference to everything in the resources folder, all the IDs, 509 00:38:05,350 --> 00:38:10,060 and resultTextView. 510 00:38:10,060 --> 00:38:13,010 And just as before, resultTextView is an object, 511 00:38:13,010 --> 00:38:15,780 which means it has all these properties associated with it. 512 00:38:15,780 --> 00:38:20,610 When we use getText for the edit text for this text field or the text view 513 00:38:20,610 --> 00:38:23,980 we can actually use setText, 514 00:38:23,980 --> 00:38:26,960 and then we already found the message from the intent, 515 00:38:26,960 --> 00:38:35,760 so I can say message, and this will actually set the message. 516 00:38:35,760 --> 00:38:41,820 >> It take a moment to launch, and then we can see whether or not it crashes, 517 00:38:41,820 --> 00:38:48,670 but any questions about that process right there, that sort of interaction? 518 00:38:48,670 --> 00:38:51,490 In many ways it's straightforward. 519 00:38:51,490 --> 00:38:53,960 The reason I'm going through something relatively simple 520 00:38:53,960 --> 00:38:57,330 is, again, because you get to see the interaction between the layout, 521 00:38:57,330 --> 00:38:59,520 the source code, how you reference things 522 00:38:59,520 --> 00:39:07,680 and maybe get a little exposure to how Java works there. 523 00:39:07,680 --> 00:39:16,500 If I actually type something in here, like ohai, and then I click the button, 524 00:39:16,500 --> 00:39:22,260 it launches a new activity, and that text view says ohai. 525 00:39:22,260 --> 00:39:27,640 That's very simple, but hopefully after that example 526 00:39:27,640 --> 00:39:32,310 you sort of see how this interaction works, and now that mess of files 527 00:39:32,310 --> 00:39:36,870 on the left side for all these project folders, hopefully you know where to look now. 528 00:39:36,870 --> 00:39:38,700 This is your source code area. 529 00:39:38,700 --> 00:39:41,760 >> Layout is really the only thing you'll deal with until you include images 530 00:39:41,760 --> 00:39:46,370 and the drawables or whatever, and values, that will include strings, 531 00:39:46,370 --> 00:39:51,960 as we mentioned, and then styles is something that's sort of like CSS. 532 00:39:51,960 --> 00:39:56,410 Address it when you want, but until you feel comfortable with adding extra flair 533 00:39:56,410 --> 00:40:02,520 to your application there's no need to worry about it too much. 534 00:40:02,520 --> 00:40:07,410 Just to wrap up one other thing you will definitely want to include— 535 00:40:07,410 --> 00:40:13,010 assuming you want to integrate cool features and save yourself some extra coding— 536 00:40:13,010 --> 00:40:16,460 would be to include 3rd party libraries. 537 00:40:16,460 --> 00:40:19,840 Here's the little Android guy with the Sherlock Holmes hat. 538 00:40:19,840 --> 00:40:23,040 This is an action bar Sherlock library, 539 00:40:23,040 --> 00:40:25,390 which basically means that action bar you saw earlier, 540 00:40:25,390 --> 00:40:29,590 which is now standard in Ice Cream Sandwich and Jelly Bean, 541 00:40:29,590 --> 00:40:33,100 if you want to bring that sort of user experience to users on older versions 542 00:40:33,100 --> 00:40:39,290 and save yourself from worrying about how the user is going to navigate 543 00:40:39,290 --> 00:40:42,490 on older versions when that thing isn't there you can include this library 544 00:40:42,490 --> 00:40:46,770 and then you just have to reference—rather than the default Android 545 00:40:46,770 --> 00:40:50,250 libraries for the action bar, you reference this support library. 546 00:40:50,250 --> 00:40:53,090 >> Things like Scoreloop, it's great if you're going to include a game. 547 00:40:53,090 --> 00:40:57,280 It lets you add leader boards and achievements to games. 548 00:40:57,280 --> 00:41:00,900 Admob is a way of putting ads into your applications 549 00:41:00,900 --> 00:41:02,900 in case you want to make a little bit of money off of it. 550 00:41:02,900 --> 00:41:05,730 Of course, there is Facebook and Twitter for integrating there 551 00:41:05,730 --> 00:41:08,170 if you want to share things easily, et cetera. 552 00:41:08,170 --> 00:41:15,120 Dropbox, likewise, and Google Analytics is that chart looking one there. 553 00:41:15,120 --> 00:41:19,070 That will be very important if you actually want to see who is interacting 554 00:41:19,070 --> 00:41:25,800 with your app or who it is, how they interact, et cetera. 555 00:41:25,800 --> 00:41:30,650 Google, by default, tells you a lot of statistics about who has installed your app 556 00:41:30,650 --> 00:41:35,020 and what phones they have, what operating system versions they have. 557 00:41:35,020 --> 00:41:38,080 But if you want to see who is using it on a daily basis and how they use it 558 00:41:38,080 --> 00:41:42,860 then you'll want to include some sort of tracking system like that. 559 00:41:42,860 --> 00:41:44,990 >> When you're ready to distribute your app 560 00:41:44,990 --> 00:41:46,930 I don't need to go into that in too much depth. 561 00:41:46,930 --> 00:41:49,090 Click on the link here, Google Play. 562 00:41:49,090 --> 00:41:51,080 It used to be the Android market. 563 00:41:51,080 --> 00:41:53,470 They rebranded everything with Google Play, 564 00:41:53,470 --> 00:41:55,230 so now you submit to that. 565 00:41:55,230 --> 00:41:57,140 It's very straightforward. 566 00:41:57,140 --> 00:42:01,070 You have to include a description, some screenshots. 567 00:42:01,070 --> 00:42:05,660 Like I said, there's no approval process like iOS. 568 00:42:05,660 --> 00:42:07,270 And where to go from here? 569 00:42:07,270 --> 00:42:09,450 I showed you a really simple example. 570 00:42:09,450 --> 00:42:13,270 Hopefully if it seems terribly simple 571 00:42:13,270 --> 00:42:15,740 then that's probably a good sign. 572 00:42:15,740 --> 00:42:20,080 If you at all felt a little bit confused or unsure about exactly what I was typing 573 00:42:20,080 --> 00:42:26,160 that's also fine, but from here go to Google Guides. 574 00:42:26,160 --> 00:42:28,330 This is a great place to start. 575 00:42:28,330 --> 00:42:31,460 It will talk you through what they expect in terms of the application design, 576 00:42:31,460 --> 00:42:33,380 how the users normally interact with it. 577 00:42:33,380 --> 00:42:36,450 It's much more free form than iOS, I'd say, 578 00:42:36,450 --> 00:42:41,330 which it seems like table views are—I don't know, that's their bread and butter. 579 00:42:41,330 --> 00:42:44,690 Everything is a table view sliding up and down. 580 00:42:44,690 --> 00:42:48,160 With Android they definitely encourage people to look at it from 581 00:42:48,160 --> 00:42:51,720 a whole bunch of different perspectives. 582 00:42:51,720 --> 00:42:56,000 >> When you include the software development kit in this ADT 583 00:42:56,000 --> 00:43:03,010 by default you have a whole bunch of sample projects in Android. 584 00:43:03,010 --> 00:43:05,470 Right here, Android, Android Sample Project. 585 00:43:05,470 --> 00:43:11,870 If I click on that, then I can choose a version for which of the sample projects apply. 586 00:43:11,870 --> 00:43:13,300 There's a whole bunch of different things here. 587 00:43:13,300 --> 00:43:16,690 It starts off with action bar compatibility, 588 00:43:16,690 --> 00:43:24,610 accelerometer play, backup and restore, Bluetooth, gesture builder. 589 00:43:24,610 --> 00:43:28,930 JetBoy is a sample game they give you, as is LunarLander. 590 00:43:28,930 --> 00:43:31,250 There are a whole bunch of sample applications here, 591 00:43:31,250 --> 00:43:35,290 and they're all targeted at different aspects of Android's API, 592 00:43:35,290 --> 00:43:41,520 and the idea is that as you make your application 593 00:43:41,520 --> 00:43:44,110 you don't need to learn everything about Android to make an Android app. 594 00:43:44,110 --> 00:43:47,300 You only have to learn about the parts that you're actually going to use. 595 00:43:47,300 --> 00:43:49,340 Look at these sample libraries. 596 00:43:49,340 --> 00:43:51,510 Feel free to copy and paste the code and reapply it. 597 00:43:51,510 --> 00:43:53,850 It's a great thing to look at more difficult code 598 00:43:53,850 --> 00:43:57,060 and try to understand it, which is of course why in CS50 599 00:43:57,060 --> 00:44:01,510 we often give you large amounts of source code. 600 00:44:01,510 --> 00:44:04,620 Look at some of these if you're going to use similar libraries 601 00:44:04,620 --> 00:44:08,630 and then apply it to your own code. 602 00:44:08,630 --> 00:44:10,430 And of course, there are tutorials. 603 00:44:10,430 --> 00:44:12,690 Search for tutorials. 604 00:44:12,690 --> 00:44:17,020 I found a lot of good ones online which can bring you up to speed 605 00:44:17,020 --> 00:44:20,080 with what fragments really are and how you can use them. 606 00:44:20,080 --> 00:44:22,240 >> Some of these new design things, like fragments, 607 00:44:22,240 --> 00:44:26,840 you really don't have to be worried about unless you're making a 608 00:44:26,840 --> 00:44:30,560 pretty large in scope project. 609 00:44:30,560 --> 00:44:32,510 They are things that Google is trying to phase in. 610 00:44:32,510 --> 00:44:35,570 Stackoverflow is of course your friend. 611 00:44:35,570 --> 00:44:37,930 Hopefully I sold you a bit on Android, 612 00:44:37,930 --> 00:44:41,640 realizing it's much more easy to approach in terms of just the coding language. 613 00:44:41,640 --> 00:44:45,970 Java is I'd say much easier to understand than Objective-C. 614 00:44:45,970 --> 00:44:50,780 Android is growing I think much more than iOS. 615 00:44:50,780 --> 00:44:54,890 I mean, the data is there too, aside from what I think. 616 00:44:54,890 --> 00:44:56,920 It's going to be around for a while, 617 00:44:56,920 --> 00:45:00,880 and they are a few years behind in terms of their design cycles, 618 00:45:00,880 --> 00:45:04,690 so it's sort of understandable that they're still working through some of these 619 00:45:04,690 --> 00:45:09,080 consistency questions, like this action bar is a new thing for them still, 620 00:45:09,080 --> 00:45:16,010 sort of creating similar user experience across all their apps. 621 00:45:16,010 --> 00:45:20,650 Hopefully you can go forward now and open up Eclipse 622 00:45:20,650 --> 00:45:22,870 and make your own Android apps. 623 00:45:22,870 --> 00:45:30,240 >> Does anyone have any questions before we wrap up about anything? 624 00:45:30,240 --> 00:45:34,410 I can tell you the answer is 42. 625 00:45:34,410 --> 00:45:41,990 Well, happy coding everyone, and good luck. 626 00:45:41,990 --> 00:45:44,990 [CS50.TV]