1 00:00:00,000 --> 00:00:06,000 like i've mentioned perviously, activities are one of the primary building blocks for your applications. 2 00:00:06,000 --> 00:00:10,000 and an activity is simply a single screen in your application. 3 00:00:10,000 --> 00:00:16,000 so let's say, an email client, if we have the list of emails, that's one activity, 4 00:00:16,000 --> 00:00:20,600 when we have the message details for particular massage, that's another activity 5 00:00:20,600 --> 00:00:24,500 the compose screen, where you write a new massage, that's another activity. 6 00:00:24,500 --> 00:00:29,000 so it's really important to grasp and learn the fundamentals of activities. 7 00:00:29,000 --> 00:00:36,000 so i'm gonna create a new android project, and i'm gonna call it LearnActivities 8 00:00:36,000 --> 00:00:42,500 we're gonna now save it in the default work space, again we're gonna be targeting gingerbread, 9 00:00:42,500 --> 00:00:47,000 for the name of my application, i'll just call it Activities, 10 00:00:47,000 --> 00:00:53,700 package : com.leebrimlow.activities 11 00:00:53,700 --> 00:01:00,000 we're gonna create a main activity, which is the activity that user sees when they first launch your app, 12 00:01:00,000 --> 00:01:09,800 and what i like to do is i always call that Main. min SDK version which is targeting gingerbread, and i'm gonna click finish. 13 00:01:09,800 --> 00:01:17,000 so like i showed you before at this point we actually have a fully working application, the HelloWorld application, 14 00:01:17,000 --> 00:01:25,900 we have our Main.java which is the file that actually extends the activity class that represents our initial main activity. 15 00:01:25,900 --> 00:01:37,200 and for the actual content, the visual content, it's referencing a file, called main, which is in the layout folder in my recourses, 16 00:01:37,200 --> 00:01:46,000 so if i go to resources, layout, there we see that main.xml, and by default it just has HelloWolrd, Main! 17 00:01:46,000 --> 00:01:51,700 just has some text in side of a textview, so i'm gonna run that just to make sure it's working correctly, 18 00:01:51,700 --> 00:01:58,000 again we wanna run it as an AndroidApplication, and we'll lunch up the emulator 19 00:01:58,000 --> 00:02:02,000 and we can see it's running correctly. 20 00:02:05,000 --> 00:02:11,400 so i quickly wanna point out a couple of things in the android manifest file, so we could see here in the xml view 21 00:02:11,400 --> 00:02:17,500 inside of our application tag, we have our activity. now one of the things you'll see inside of here 22 00:02:17,500 --> 00:02:24,500 is this intent-filter. we're gonna be talking about intents in a lot more detail, i'm coming up in another movie, 23 00:02:24,500 --> 00:02:29,000 but this basically tells the android system by putting in this action 24 00:02:29,000 --> 00:02:36,000 that this is the activity that should be launched when the application is first launched, 25 00:02:36,000 --> 00:02:44,700 and it does that by putting in this "android.intent.action.MAIN" so this is basically advertising the android system, 26 00:02:44,700 --> 00:02:48,500 hey when you lunch me, lunch this activity first, 27 00:02:48,500 --> 00:02:55,000 and it's this main activity that we have. so again we're gonna get into more details about intents in a little bit. 28 00:02:55,000 --> 00:03:03,000 but let's say that i wanna add another activity to my application because usually your applications are gonna have multiple activities, 29 00:03:03,000 --> 00:03:08,000 we basically need to recreate these two files, for different activities. 30 00:03:08,000 --> 00:03:16,000 so first we need to create another java file that extends Activity. so i'm just gonna right click in my main package, 31 00:03:16,000 --> 00:03:24,000 and i'm gonna select new->class, and i'm gonna call this one Second, so i'm gonna click browse. 32 00:03:24,000 --> 00:03:33,800 and here i'm gonna want to extend activity which is in android.app package. click OK. 33 00:03:33,800 --> 00:03:35,000 and i can just click finish. 34 00:03:36,000 --> 00:03:41,000 so now it's created another java file called Second, which extends activity. 35 00:03:41,000 --> 00:03:50,000 but i also need to override, this onCreate method, so that i can actually set which UI i want to put into this activity, 34 00:03:50,000 --> 00:03:59,000 so i can just come inside of here, and give myself some space, start writing oncreate and hit ctrl+space. 35 00:03:59,000 --> 00:04:04,000 and now we can this first option, will create the override method for me. 36 00:04:04,500 --> 00:04:12,000 now i haven't actually created a new xml file, so i don't anything to put here yet, so that's the next step. 37 00:04:12,000 --> 00:04:19,000 i need to create an xml file, similar to main.xml which is gonna hold the UI for the Second activity. 38 00:04:19,000 --> 00:04:26,000 so this third button here in the toolbar opens up a wizard to help create a new android xml file. so i'm gonna click that. 39 00:04:26,500 --> 00:04:33,000 and here i need to give it a name so i'm gonna call this second.xml. 40 00:04:33,000 --> 00:04:41,700 now the naming conventions here are really strict so you can't have a capital letter here, you can't have different symbols, 41 00:04:41,700 --> 00:04:49,000 and it has to be lower case, so i'm gonna put second.xml. and now it's asking us what type of resource would you like to create. 42 00:04:49,000 --> 00:04:55,000 well i wanna create a layout resource, very much like main.xml so i'm gonna choose layout. 43 00:04:55,000 --> 00:04:59,500 and now it's asking me what do i want the root element for that xml file to be. 44 00:04:59,500 --> 00:05:08,000 essentially what is gonna be my root layout container, so there are a bunch of different layout options that you have in android, 45 00:05:08,000 --> 00:05:18,000 and we're gonna get to that also in a future movie. we're gonna leave the default as linear layout which is pretty much the simplest most standard layout option 46 00:05:18,000 --> 00:05:22,000 as we add things, they laid out in a linear fashion one after another. 47 00:05:22,000 --> 00:05:31,500 so we're gonna click finish. and now it's created a new xml file and here we are in the graphical layout view. 48 00:05:31,500 --> 00:05:41,000 so from this point i can actually just start adding various UI controls to this. so let's say i want very much like the main.xml 49 00:05:41,000 --> 00:05:47,700 i just want this one to have a textview, so i'm gonna drag that out here, we can see there it is. 50 00:05:47,700 --> 00:05:55,000 and in here i can actually go into my properties, (let me actually just make this stuck to the side there). 51 00:05:55,000 --> 00:06:01,000 now i can set all the properties for this textview. so if i scroll down, 52 00:06:02,000 --> 00:06:10,000 i can see one of the properties here is actually the text property or what's gonna be inside of this textfield, what is the text gonna say, 53 00:06:10,000 --> 00:06:18,000 so i'm just gonna change that to "This is the second activity". 54 00:06:19,000 --> 00:06:24,500 and that's all what we're gonna do at this point, so it's gonna be very simple when we get to this activity, 55 00:06:24,500 --> 00:06:29,000 it's just gonna display this massage. if we wanna go and look at what the xml looks like, 56 00:06:29,000 --> 00:06:34,000 of course we can go back and look at that, and you'll notice that things are not wrapped nicely, 57 00:06:34,000 --> 00:06:42,000 well one thing we can do is to right click, goto Source, and then cleanup document, and then just click OK. 58 00:06:42,000 --> 00:06:52,000 and it's actually going to make things be laid out a little bit better. but again very much like main.xml we have a linear layout with a textfield 59 00:06:52,000 --> 00:06:59,800 OK so now that we have the second activity, but we need a way to actually be able to lunch it, because remember when we first come into the app 60 00:06:59,800 --> 00:07:06,500 we're coming to this main activity, so what we're gonna do here is to first i'm gonna delete this textview. 61 00:07:07,000 --> 00:07:13,500 and now we're gonna put a button in here, so i'm just gonna drag this button into the activity. 62 00:07:14,000 --> 00:07:25,000 if we wanna look at the properties here, we can. the ADT tools will actually automatically give any controls that you drag out here an ID, 63 00:07:25,000 --> 00:07:30,500 that you can actually come in and customize this id which is how you reference the control throw out your project, 64 00:07:31,000 --> 00:07:34,000 but for right now we're gonna leave it at the default. 65 00:07:35,000 --> 00:07:42,500 so now i have this button, now i need to actually write the code so that when the user taps this button it's gonna launch the second activity. 66 00:07:43,000 --> 00:07:49,500 so i'm gonna go to my main.java file. and under where i set the content view, 67 00:07:50,000 --> 00:07:56,000 the first thing i need to do is to get a reference to that button control that i put in the xml. 68 00:07:56,400 --> 00:08:03,500 so to do that i'm gonna create a button property, and i'm just gonna call it b, to keep it simple. 69 00:08:04,000 --> 00:08:15,000 and in the way which we can get a reference to UI components in our xml is to use the findViewById method, 70 00:08:15,000 --> 00:08:24,000 and this will actually go out if we give it an Id, and return us a reference to that control, so in order to actually get the id, 71 00:08:24,500 --> 00:08:34,400 we're gonna wanna go to R.id there you can see our button control with that name that was given by default, 72 00:08:34,500 --> 00:08:39,000 so this will now return a reference and save it in this b property that we created, 73 00:08:39,000 --> 00:08:48,700 if we hit ctrl+1 here, we can see it's giving us an error, it will give us a quick fix which is to add cast to this button, 74 00:08:49,100 --> 00:08:57,500 so we're basically casting it to a button control, so we now have our reference to that button stored in this b property. 75 00:08:58,000 --> 00:09:03,500 so the next thing we need to is to set up the onClick listener, so that when ever the user clicks that button, 76 00:09:03,500 --> 00:09:11,000 we can respond to it and actually cause something to happen in our application so the way which we can do that is to say, 77 00:09:11,000 --> 00:09:20,000 b.setOnClickListener we can see it's the first choice here, so i'm gonna select that and now inside of here what we're gonna do 78 00:09:20,000 --> 00:09:27,400 is to create a new onClickListener so we're gonna say new, and i'll just start typing it, 79 00:09:27,500 --> 00:09:36,700 and then hit ctrl+space. and we can see it's gonna create an anonymous inner event handler in which we can response to this event, 80 00:09:36,800 --> 00:09:43,000 so when i click this we can see it's automatically created that stop for me, again if i hit ctrl+1 81 00:09:43,000 --> 00:09:49,000 i'll be able to get some quick fixes here and i just wanna import the onClickListener class, 82 00:09:51,000 --> 00:09:56,000 and i'll come down here, and put a semicolon. so now inside of this onClick method, 83 00:09:56,000 --> 00:10:05,000 this is where i can actually do something when the user has clicked that button, so all i'm gonna do is to lunch this other activity 84 00:10:05,000 --> 00:10:13,300 so the way in which we start activities, is to actually call the startActivity method, 85 00:10:13,700 --> 00:10:23,000 so to this method we pass in an instance of the intent class, and the intent class which we're gonna cover a lot more in a future movie, 86 00:10:23,000 --> 00:10:30,000 but this actually just broadcast, hey i want to activate this particular component or this activity. 87 00:10:30,000 --> 00:10:36,000 so we're just gonna do an inline here, i'm gonna create a new intent. 88 00:10:36,000 --> 00:10:45,000 now there's a bunch of different constructer overrides here that i can pass in, the one we're gonna use specifically to lunch activities is this one, 89 00:10:45,000 --> 00:10:53,000 in which we pass in a context and then we pass in which class which represents the activity do we want to call? 90 00:10:53,000 --> 00:10:59,700 so now when it comes to context, this is something that you'll have to use quite a bit as you doing android development. 91 00:10:59,700 --> 00:11:09,000 so any time we request to the system to launch something, we send in a context so that, that component knows who is requesting it essentially. 92 00:11:09,000 --> 00:11:15,700 so now there's a few different ways to get the context, but because we're in an anonymous inner method here, 93 00:11:15,700 --> 00:11:22,500 the easiest way to get the content is to just say Main.this 94 00:11:22,500 --> 00:11:31,000 which is referencing our main file here, and this basically tells the system that hey the main activity is requesting this action. 95 00:11:31,000 --> 00:11:39,000 so the next thing we send in is the class of which activity we want to launch, well that is Second 96 00:11:39,000 --> 00:11:44,000 and then we give it the class property, and then we can close that. 97 00:11:44,000 --> 00:11:50,000 so really quick just to recap, we've gotten a reference to our button which we define in our xml here, 98 00:11:50,000 --> 00:11:56,000 we've set up the onClickListener, we just did it as an anonymous inner type event handler here, 99 00:11:56,000 --> 00:12:01,000 there are other ways to it as well, which we'll cover when we get to talking more about button controls, 100 00:12:01,000 --> 00:12:10,000 when the user clicks, and you'll notice in android it says click event though the user typically will be doing it with their finger, it's the same thing. 101 00:12:10,000 --> 00:12:17,000 we're just gonna start a new activity we're passing in this intent object, which basically is saying who is requesting this, 102 00:12:17,000 --> 00:12:23,000 well it's this activity right here which is Main. and then which activity we'll want to launch. 103 00:12:24,000 --> 00:12:29,000 so let's go ahead and launch that. and we'll save these resources. 104 00:12:31,000 --> 00:12:33,000 and let's go to the emulator. 105 00:12:35,000 --> 00:12:43,000 so we can see it's launched now and when i click on the button, now you can see what happens when i launched this is we actually get an error, 106 00:12:43,000 --> 00:12:47,000 and it says Sorry the application Activities has stopped unexpectedly. 107 00:12:47,000 --> 00:12:54,000 so now this is gonna happen to you often during your development, so how do you respond to this, and how do you figure out what's wrong? 108 00:12:54,000 --> 00:13:03,000 well let's go back to Eclipse, now what we wanna do is to go to a deferent perspective, so right now we're in the java perspective. 109 00:13:03,000 --> 00:13:11,000 if we click this and then go to other. we can see that there's one specific to android called DDMS. 110 00:13:11,000 --> 00:13:21,000 and this contains a bunch of different tools in which you can monitor how your application are actually running on the emulator or actually on a real device. 111 00:13:22,300 --> 00:13:29,000 one of the most useful areas is LogCat so as your applications are running in the emulator or a device, 112 00:13:29,000 --> 00:13:36,000 there are constantly sending out massages about things, and we can already see that we have a big one in red, which is generally not a good thing. 113 00:13:37,000 --> 00:13:44,000 so if we scroll up here we can see what happen, FATAL EXCEPTION : main ActivityNotFoundException. 114 00:13:44,000 --> 00:13:50,000 so it couldn't find that activity that we created, so why was that? 115 00:13:50,000 --> 00:13:56,000 it's because we haven't actually added it to the manifest file. so let's go back to the manifest file, 116 00:13:56,000 --> 00:14:03,000 remember any activity that exists in your application, it needs to be defined here in the manifest file. 117 00:14:03,000 --> 00:14:07,000 so all i'm gonna do is to create a new activity here. 118 00:14:10,000 --> 00:14:15,000 and i'm just gonna start typing name and then hit ctrl+space give me the code hinting, 119 00:14:16,000 --> 00:14:25,400 now for name we actually need to reference the name of it, so of we hit period that basically gets us to the end of our default package here, 120 00:14:25,400 --> 00:14:35,000 and i'm just gonna type in Second and that's all we need to do actually inside of here because we don't need to define any intents. 121 00:14:35,000 --> 00:14:39,000 that's all we need to do with this point just to define this activity. 122 00:14:39,800 --> 00:14:44,000 OK so let's go ahead and relaunch this and go back to our emulator. 123 00:14:44,400 --> 00:14:46,000 and let this load-up. 124 00:14:47,000 --> 00:14:58,800 OK so i'm gonna click this button now, and now you can see it's actually gone to the second activity but where is the textview which says hey this is the second activity? 125 00:15:00,000 --> 00:15:08,000 well if we go back to our java file. you'll notice the difference between this and the main one is that we haven't set the contentView 126 00:15:08,000 --> 00:15:13,000 which basically tells the activity which xml file to use for it's visual content. 127 00:15:14,000 --> 00:15:24,000 so what we're gonna do is to call setContentView and then give the id of the layout file that we wan to use. 128 00:15:24,000 --> 00:15:33,000 so to get that we say R. and then go to layout and we wanna use the second.xml file here. 129 00:15:33,000 --> 00:15:38,000 and with that we should be done. so let's launch it one last time. 130 00:15:42,000 --> 00:15:51,400 there it is, we click the button and now it's actually showing us the second activity with the visual content from that second xml file. 131 00:15:52,000 --> 00:15:59,000 so again that's what it takes to add a new activity to your application. again you're gonna be creating a java file, 132 00:15:59,000 --> 00:16:03,000 if your gonna be using xml for the UI, you're gonna need to create that. 133 00:16:03,000 --> 00:16:08,000 and then a really important part is that you need to define it in your manifest file. 134 00:16:08,000 --> 00:16:15,000 so we're gonna be getting a lot more into activities and how they work with intents, but that's a basic overview of creating activities. 135 00:16:15,000 --> 00:16:17,000 AndroidApp.blog.ir