 Okay, so thank you all for coming. So today this event is all about getting people interested and getting people started in writing Android apps. Okay. So those people who haven't met Android before, you're not going to be writing industrial applications by the end of the afternoon, but hopefully you will have learnt all the tools that you need to start on your journey and you will have caught the bug. For those of you who have had a play before, hopefully you will have learnt something new and you'll have met other people also interested in it so you can go away and learn together. The first thing I'd like to say is the people you're going to be learning from today, one of them's here and we'll meet some of the other ones later, but most of them are sitting next to you and behind you and in front of you. So we're going to be learning from each other whilst we go through the tutorial and everything with each other. So if at any point during the afternoon you have a problem, you don't know what's going on, then turn to the person beside you and say can you help me and they might be able to help you, they might turn to the person next to them and say can you help me and eventually they'll find somebody with the answer hopefully. So that's the kind of atmosphere we want going on this afternoon. So we're not teaching you, we're facilitating you learning, we're facilitating you having fun as well, that's the important thing. Cool. So if we just introduce ourselves, firstly we've got Matt, our resident expert, do you want to just say a few things about yourself? Yeah I wouldn't say I'm an expert but I've been doing Android development for the past year on placement so I'm currently a third year student. Last year I spent down in Brighton, I was working for a small company Rebo and we did apps for clients so we did Tesco and people like that. Also done some of our own internal stuff on Android and I got a pretty good idea of how it all works so that's me. So that's where Matt's coming from. Martin also here, do you want to say anything about what you've done in the past with Android? Well this is my first year with Android and I've created an app which covered the most basic concepts covered in Android and I'm just keep going and learning. So Martin's an example of a person who wanted to be able to do something, went away, learnt it, made mistakes and learnt from those mistakes so hopefully today you'll be able to learn from his mistakes as well as your own. The other three people on there are me, Danielle and Dragos who are sitting at the back. We're from ManUp so if you haven't heard of ManUp, a quick plug, we're Manchester Ultimate Programming, we hold events like this and weekly talks where we try and have fun with computers and if you want to know anything more go to our website, our Facebook page and ask us questions. Quickly summary of what's going to happen today so everybody's on the same page. We're going to have a talk which is why we're in this room because this is a room for giving talks and it's going to last about half an hour. Matt's going to give you an overview of Android, how the different bits fit together, some grounding so when we go and start running through the tutorial and start doing some more work, we have a shared understanding. When we move through to LF 15 we're going to split it into different groups depending on what people want out of the session so we'll have a group for people who haven't done anything before walking through that door and we'll help you install what you need to install, get things set up. We'll also have another group for people who want to start running through the tutorials going through and we'll also have a third group for people who've had a play before and want to look at some more interesting topics, want to start doing some developments and that kind of thing. So can I just have a quick share of hands? Who's an expert in Android? So nobody, that's good. Who's had a play before? So quite a few of you had a play before. That's good so we're going to give you the opportunity to get together and play together. Who's managed to go through our installation instructions and install some things on their machine? A lot of you, good. Who hasn't? Who's just walked through those doors and hasn't done anything? Include yourself in that group if you've tried and failed the instruction sheet. Okay, one more, two more people, three more people, good. Okay, so it looks like we're going to have a nice mix of different things going on. Brilliant. So you might get to different stages in this depending on what you've done before getting here. If this is very successful then we might meet again and give people an opportunity to build on us. This is kind of a toe in the water to see what we can get out of this and I hope everybody has fun. That's the most important thing. So I'm going to hand over to Matt now who's going to talk about things that you're actually interested in hearing about. Okay, thank you very much. Let's do this quickly. Okay, so what I'm going to talk today is mainly just a basic overview of Android. So it's not going to be too much about how you actually code stuff. It's going to be more just an overview of how the system fits together, all the different components and just pointing out bits that you should probably look more into in the next couple of hours. So there'll be a little bit of code shown on the screen, but I'm not going to go through it too much because the documentation is actually pretty good for doing that and in half an hour I don't think we've got much time to actually do some code. Okay, so let's quickly go through the basic structure of an Android app. So at the very top of it you have the application. So each component has its own life cycle. So what that means is it all you've got a Java class and it'll have methods that are called specific times. So you normally have like on create, on pause, on resume and on destroy. So on create is when it first gets created. So in the applications case is when you first launch it you'll get that first I'm being created. So that's a perfect opportunity to start kicking off some of the stuff you need to do in the background, some of the bits you need to do for your app. Below that you've got each activity. So who actually uses Android as their day-to-day phone? Cool. So each of the screens that you see, each of those is essentially a different activity and for all intents purposes you can class it as a separate application almost. So they each have their own life cycle. So when you first open a screen it'll go on create. So it creates the view and starts setting out all the layout and stuff and then it'll go through a series of other methods which set up the class and make sure it's ready to go. So you have multiple of these in each application and you sort of navigate between them. Below the activity you have what are called fragments. So has anyone got an Android tablet? A few people, no Tom has. So the fragments are basically the different elements on the screen. So you've got, I'll probably run through an example now quickly. This thing has not many people have seen it. Visualise the left. Cool. So I use this, that one. Yeah, cool. So if I use the calendar as an example, so on this screen here you've got one big fragment at the top which is the bit that you interact with there. You've also got another bit of UI here and another bit there. So you can think of it as encapsulating a single bit of UI within one screen. So if I look at the same application, it's exactly the same code, running on my phone. Hopefully you can see that all right. But that is basically the top bit that you saw on the application. So you can encapsulate these bits of UI as a single class and then reuse them in different parts and different configurations depending on the device. Okay. And then below that you've got the views. So these are each of the individual elements on the screen. So Android's got quite a lot of built-in elements but the most obvious ones are things like text views, image views, edit text which is like a text box, date pickers, all this sort of thing is all built in. So you can use them in your fragments. Okay. So another integral part is the manifest file. So this one, when you first create an application you'll get a file that looks a bit like this. Can everyone see the screen? Okay. I don't need to take the lights down a bit. Cool. Okay. So the manifest file is just an XML file which tells the operating system what is in the app. So this is the one that gets generated for you when you first create a basic app. So you can see it describes the version code and the version name, gives a package name, the icon you want to use on the launcher, the name for the app and then each of the individual activities will go there. So each of the individual screens you've got to define them there. I'll go more, a lot of that won't make sense until you start playing with it but hopefully I'll go through bits of that now. So activities. So these are really integral to Android so each of them have their own life cycle like I said. So you can treat them almost as a separate application. There shouldn't be very much shared state between them. So if you've got like a data object you shouldn't really pass the whole class between two activities because you'll end up with the operating system keeping hold of both screens at once using more memory and causing your phone to be unresponsive. So I'll go through in a minute but you've essentially got a message passing system instead. So activities like I said should normally contain fragments which handle the actual UI. The activity should just be the link between those fragments depending on what sort of device they're on. You might find yourself when you're building for phone and tablet and even the bigger 10 inch tablets as well you might have different paths that the user goes through to do the same task. So on the bigger devices it might all be on one screen. On the smaller ones like the phone they might have to move between three different activities and that's something you'll see when you start building with it. You'll be able to see that'll become more obvious. Okay so I talked about the life cycle a bit. Is that clear enough? Can everyone read the little text? Okay so this is for a single activity so a single screen of the application. So first bit the activity is launched and you'll get that with a method on create will be called. So that's your call to start setting things up. Start setting the layout up and getting ready for the view to actually be shown. You'll then get on start which isn't used too often normally you just leave that as it is but that's the one that's called afterwards. On resume is called the first time it's set up but also if the user goes away from activity and then comes back. So if they go away they click on a link away for a minute or whatever and then they come back you'll get on resume. So on the opposite side of that there's also on pause. So the on pause is if you've got something running in the background or you're holding on to a database cursor a database object you can then close that when they're when you get called on pause because the user's no longer looking at your activity they've gone away so you don't need to hang on to it you can free that memory and then when they come back in on the on resume you can then reopen it and it's a way of making the whole system more responsive by you being as smart with your resources as possible. So after that you've got the on stop this sort of going down and then when the activity is completely destroyed and wiped from memory you'll get undistroyed. One strange thing with this is when you rotate the device so when you go from portrait to landscape you'll actually go through this entire lifecycle so they're in portrait you rotate and at that point it goes on pause on stop on destroy until you've destroyed everything and then it will recreate it completely from new. So it's a little quirk of android that seems a bit stupid at first but what it allows you to do is to have different layouts and different functionality for different orientations of the device. What you can do though is I don't think it's on no it's not on this diagram but you can save some state in between so like the position in a list or actually save some of the data so when they come back into the correct the orientation they want the data's already there you don't have to completely reload everything so you can do stuff like that but by default it will essentially destroy the screen and recreate it so move on from there okay so again this is from the very basic um when you first generate a um android project you'll get a basic activity so this is literally all it does so if you've seen Java before you've probably seen something that looks exactly like this you've got your package name you've got the um import statements and then you'll see the class that you've created extends activity so that's a built in android class specific to android and that has a lot of the basic functionality built in um and then you're overriding the oncreate and then you can see here setting the content view so at this you're setting a specific layout to be the view that's going to be shown I'll come more into layouts in a minute um you see you also need to call the super so that's calling the super class um so it runs its bit of oncreate as well if you don't do that you'll get a crash and you'll get a tiny little error in the error log and at first you'll be confused because it doesn't pop up anything obvious on the screen but if you read back you'll see that you've just missed that one line it's amazing how often you do that okay so the layouts these are defined as xml files so they basically they basically layout everything on the screen as part of that you've got the layouts it's a bit confusing the um like layout containers and the actual views themselves the containers there's quite a lot but the ones you'll use the most are the linear layout the frame layout and relative layout I'm not going to go too deeply into them I think I'm going to show you the basics of them but these are the ones that you'll see the most so if I go through I think I've got okay so this is an example of a linear layout so hopefully from the name you can guess what it does so you've got a view up here and then in this case you'll see the orientation horizontal so it'll put them next to each other horizontally you can also have that vertically and it'll lay stuff out vertically it's a really simple one to use so that you have your linear layout then inside I've got these two fragments so they're going to be laid out next to each other um you'll also see you've everything's prefix with android this is sort of a quirk of xml which is why I really hate xml because it's quite verbose but you generally have to do you have to do that with everything and you've got layout width and layout height these are required for every single element you put in if you don't do this you won't get any errors at compile time but when you try and show the the layout it will just crash it'll complain so the options you've got for that are fill parent um wrap content and putting a specific width in um the fill parent does what it says the wrap content will see what the content is and make it exactly the right size um and then putting a specific width in will um make it that width a little bit of a quirk on here the linear layout has got something called a weight so basically you define a ratio that you want the views to have so in this case fragment two is three with the weight of three fragment one is the weight of one so you'll get that nice proportion and for that to work you need to set the width to zero which is really unintuitive but that's the way it works okay so the relative layout this one is much more complicated and you see a lot of android developers not using it so it looks the same like that you've just got a relative layout still got the width and height the orientation i don't know why i've left that in there that shouldn't be there so in this case we've got three fragments so the way the relative layout works you say you put the views in and you say how they're how they're laid out relative to each other so if you look at fragment one which that top one you say you see the on line nine uh you've got a line parent left is true so it's aligned to the left of the screen and it's also aligned to the top of the screen fragment two the one below that one is aligned to the top of the screen but to the right of fragment one fragment three is aligned to the right of fragment one and below fragment two so you can see it's quite a simple language those are the sort of things you're going to be saying saying and you can do quite complicated things to do this with a linear layout you'd need lots of nested layouts and when you get to really complicated ones it starts getting quite slow because you in that case there's only one uh layer that it has to go down so this is the actual this is what it looks like when you run it so you can see fragment one to the top left of the screen fragment two to the top and to the right and fragment three taking up that space there so you can just define all these things and not many not a surprisingly few number of android developers actually use this but it's really good for getting efficient layouts you don't have to do this nesting of linear layouts which looks ugly in xml um and this is just much easier much quicker one thing i'll say as well i'll go back there's another one which i haven't put in is the frame layout so the way this works you essentially it takes up the whole screen and that any views you put in there are laid on top of each other so the one at the top goes to the back and then it goes forward i think it's that way so the best this is best to use when some of the views have got some some sort of a transparent area so if you've got an image and you want to overlay some text on it you'd use a frame layout and you do this just because it's a lot quicker android just knows that they all fill the space and it just puts them in so it doesn't have to go through the process of measuring each one and putting it in it can just slap them straight in cool okay so i mentioned before about the moving between activities so the way you generally do this is using intents so intents are basically a small pointer to the next place you want to go so generally you'll do it between like with moving between activities so i'm this activity i want to go to this one um in that case you'd have an explicit link you'd do the activity class name dot class so it's a uh java reflection sort of thing but you can also put in data uris you can do all sorts of fancy stuff with them they're also used for passing messages for system messages to activities so the system is constantly sending stuff out like the wi-fi is connected the wi-fi is disconnected the screen is turned off the brightness level has been changed all that sort of stuff you can register your interest in so you can register that you want to be told about certain events happening and your app can respond to that sort of thing but internally you're going to be using it to move between different screens so i put a quick example of that so as well as that you can be passing small bits of information generally these are primitives so things like strings um integers doubles floats all that sort of stuff and also this weird if you you'll have to look at the documentation for this but there's this idea of a parcelable which i can never say very well so you can take a big complicated class package it down and then send it through an intent and then at the other end it'll reconstruct it into a full class so it gets a bit hairy when you're doing stuff like that but you can pass data through as extras so in this example you're creating a new intent you're passing in a context which is basically android's way of getting access to certain system stuff generally an activity is a context so you can just pass your own pass yourself in which is a bit weird you then say where you want to go to so in this case we're going to another activity and a dot class the reflection bit so you've got some extras you're passing through and then you're just saying start activity and passing it the intent and that's android the android systems queue to start the process of changing screens okay so fragments just water okay so like i said before these should be used to encapsulate certain bits of the ui so generally if you've got a tablet app you might have a list on one side and then the details on the other on the opposite side you can also rearrange these depending on what sort of device you have so you can have more fragments on a big a big tablet and move between screens and all that sort of stuff okay so i think i'll show you okay so this is just one of those fragments i showed you before just a really simple example of that turn the lights down a bit too much maybe okay just a really simple example of that so you're now extending fragment instead of instead of extending an activity you've got to have this blank constructor another weird quirk of the way it's designed and in this case you've got different lifecycle methods so you've got one that's on create view so what this allow this is basically your call to actually create the view so you'll get passed in a layout inflator which you then use to inflate the xml into actual classes so you don't have to do that yourself that's all handled for you you're also passed in the containers of where you're going to live and also any any state so if the if the fragments being reconstructed then you might get some state which is used for you to set certain things in a certain way um so you see here all i'm doing is having to return a view so i'm inflating it passing in one of the resources which this layout thing so you see our layout fragment one that um that points to a specific file in the layouts folder of your resources um which is one of those layouts i showed you a couple of slides ago you're also passing in the container just so it knows where it should be living how big so this is the actual um fragment one layout file so this one's really simple one so it's the one i show you before it's just a text view with background set text color um text size and all that sort of stuff and it's just this one here so you see just how it relates okay okay so my talk really quickly about views um there are a lot of views on android so the ones you'll come across the most are text views image views buttons list views pretty much every application will have one of them um there's also grid views progress bars spinners date pickers galleries there's if you look at the documentation you can see how many subclass to view there are and there's over 50 there's like 58 59 um so there's lots of different built-in things and when you get further into making your own android app you'll start realising that the functionality is good but not quite what you want so you can start extending them so because they're just standard java classes you can extend them override some methods and change it to do what you want it to do um and you'll find yourself doing that and mashing two different so uh i don't know mashing a button and a progress bar together or something like that to create your own bit of ui okay don't think i've got any examples okay so the best way to learn them is just to have a look at the documentation you'll be able to see each of the different um methods you can pass in to make them do what you want okay so i'm going to talk really quickly just about patterns so a lot of the time i focus on doing the ui on android so i'm quite interested in making sure everything conforms to the way android expects apps to behave that sort of thing um okay so quite a lot of you use android who doesn't use androids as their phone okay quite a few so i'll take i'll do some examples okay so if we take a look here uh it's probably a bad thing on set take this away before i start showing you my emails okay there's still quite a lot there maybe don't use that app okay maps that's a better one cool okay so one of the main patterns of android is the um the action bar so this is the bit you see at the top of the screen so zoom in a bit zoom okay so at the top of this screen you can see there's a lot of different controls that you'll um you can interact with so these might differ between different devices so the same app on um the phone probably have it won't have the big search search maps option but might have button for that so on the far left of so if you look at this bit on the far left you've got the app icon this also doubles as an up arrow which is very very subtly different to to the back button on the phone um i'm not going to go through that because it's complicated and wordy but if you look at the um documentation it'll explain the differences between the up and the back there's different actions that they should perform so number two on this list is the uh a spinner so you can choose instead of using tabs um you can use what's called a spinner to navigate your app so you can you can see it with the um calendar so the spinner there changes you between different view different views so day week month and agenda um you've also got these buttons so these are commonly used functions so in this case so in the case of the calendar you've got going to um today so if you tap that it'll take you straight to today and also creating a new event so that again you're trying to bubble up the most used actions for that particular screen and you're putting them as buttons on the action bar so they're really really accessible you've also got this overflow um menu so you can see that there these are just sort of less commonly used functions of your application but you still want that want relatively easy accessible so you've got things like settings you generally put there all the google ones have the send feedback um anything that isn't something that the user's going to be using all the time but you still want to have quite direct access to okay so that's the action bar another pattern which has come out quite recent which has become more used quite recently is the um swipeable tabs so the idea is um you should be able to be on one bit of content and then swipe to the next bit so you can see this fairly easily on here you're going to swipe to the next day and that's a very common pattern you'll see on android so if i dare to go in to my email yep i don't know what this is um so you've got an email here and you can instead of going back to that list you can then just swipe to the next one so you also see it as navigation within an application so if you've got tabs you should be able to swipe between them instead of tapping on the tab but you also see it um swiping between bits of content when you started drilling down so in this case the uh gmail app okay okay so there's lots of other patterns and lots of other bits of um design related stuff this website is well worth reading so it's on the developer website and then the design tab at the top even if you even if you think you're just a developer you should still read this because it gives you a lot of insight into why everything's designed in that certain in that way and how you can make your app conform to these standards and it's well worth doing because if you release your app onto the store if they don't conform to standards they won't tend to promote them as a featured app um if you talk to the guys at google they'll just they basically just suggest that you conform to this before they'll start featuring you okay i think i'm coming near to the end okay services so this is something that's going to be this more like the back end of the app so if you've got i said before the activity shouldn't share too much state between them but obviously within an application you should you still need some sort of in the data store or um some state shared in the background where you do this is using a service so you can create this in the application um class so when you first create the application it'll start up the service and then each activity can bind to it which gives it access to the um methods uh in the service um the most common one common way that they're used is in things like music apps so the the actual music playing will be done not in the activity but in a service so that has its own lifecycle like every component on android and when you move away from the app it's still kept running um by default it runs on the same thread so you need to do um you need to either tell android in the manifest valve you want it on a separate thread or using something internally called async tasks if you don't everything will be running on the ui thread which when you're scrolling through lists and stuff is not very good you want as little as possible running there so the way you interact with it i'm not going to show you code examples as plenty on the developer website is through binding to them so this gets you a reference to the um service make sure the the service knows that you're interacting so it does it doesn't try and kill itself midway through and then you can interact with it by calling its methods the other way is using the intents so you've got um you've got an object in android that's just a service or you've got an intent service which is again another built-in class but gives you it's already written some of the functionality you need to be able to interact with with intents so you can pass messages and it will send a broadcast back to you and anyone else who's listening so you can ask it um i know how many how many items are in this data store and it'll send a broadcast back with that information i think we'll come to the end okay so it's time to go play first i was going to point out some useful links so the android developer documentation really really useful one so one thing i find myself using a lot is the reference because it's hard to remember lots of classes so you can search up here for different things and you'll get a list of all of the different things you can do with a tech a text view you can also if you use chrome go into the chrome store and you can download an android developer widget that allow sorry widget um application that allows you to type ad in the title bar then press space and then you can just type exactly what you want and it'll take you there so that's one really useful thing to install straight away because you'll be able to search through the documentation a lot easier as well take a look at the design one it's really um really well written and there's lots of information there to read i think that's it