like i've mentioned perviously, activities are one of the primary building blocks for your applications.
and an activity is simply a single screen in your application.
so let's say, an email client, if we have the list of emails, that's one activity,
when we have the message details for particular massage, that's another activity
the compose screen, where you write a new massage, that's another activity.
so it's really important to grasp and learn the fundamentals of activities.
so i'm gonna create a new android project, and i'm gonna call it LearnActivities
we're gonna now save it in the default work space, again we're gonna be targeting gingerbread,
for the name of my application, i'll just call it Activities,
package : com.leebrimlow.activities
we're gonna create a main activity, which is the activity that user sees when they first launch your app,
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.
so like i showed you before at this point we actually have a fully working application, the HelloWorld application,
we have our Main.java which is the file that actually extends the activity class that represents our initial main activity.
and for the actual content, the visual content, it's referencing a file, called main, which is in the layout folder in my recourses,
so if i go to resources, layout, there we see that main.xml, and by default it just has HelloWolrd, Main!
just has some text in side of a textview, so i'm gonna run that just to make sure it's working correctly,
again we wanna run it as an AndroidApplication, and we'll lunch up the emulator
and we can see it's running correctly.
so i quickly wanna point out a couple of things in the android manifest file, so we could see here in the xml view
inside of our application tag, we have our activity. now one of the things you'll see inside of here
is this intent-filter. we're gonna be talking about intents in a lot more detail, i'm coming up in another movie,
but this basically tells the android system by putting in this action
that this is the activity that should be launched when the application is first launched,
and it does that by putting in this "android.intent.action.MAIN" so this is basically advertising the android system,
hey when you lunch me, lunch this activity first,
and it's this main activity that we have. so again we're gonna get into more details about intents in a little bit.
but let's say that i wanna add another activity to my application because usually your applications are gonna have multiple activities,
we basically need to recreate these two files, for different activities.
so first we need to create another java file that extends Activity. so i'm just gonna right click in my main package,
and i'm gonna select new->class, and i'm gonna call this one Second, so i'm gonna click browse.
and here i'm gonna want to extend activity which is in android.app package. click OK.
and i can just click finish.
so now it's created another java file called Second, which extends activity.
but i also need to override, this onCreate method, so that i can actually set which UI i want to put into this activity,
so i can just come inside of here, and give myself some space, start writing oncreate and hit ctrl+space.
and now we can this first option, will create the override method for me.
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.
i need to create an xml file, similar to main.xml which is gonna hold the UI for the Second activity.
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.
and here i need to give it a name so i'm gonna call this second.xml.
now the naming conventions here are really strict so you can't have a capital letter here, you can't have different symbols,
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.
well i wanna create a layout resource, very much like main.xml so i'm gonna choose layout.
and now it's asking me what do i want the root element for that xml file to be.
essentially what is gonna be my root layout container, so there are a bunch of different layout options that you have in android,
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
as we add things, they laid out in a linear fashion one after another.
so we're gonna click finish. and now it's created a new xml file and here we are in the graphical layout view.
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
i just want this one to have a textview, so i'm gonna drag that out here, we can see there it is.
and in here i can actually go into my properties, (let me actually just make this stuck to the side there).
now i can set all the properties for this textview. so if i scroll down,
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,
so i'm just gonna change that to "This is the second activity".
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,
it's just gonna display this massage. if we wanna go and look at what the xml looks like,
of course we can go back and look at that, and you'll notice that things are not wrapped nicely,
well one thing we can do is to right click, goto Source, and then cleanup document, and then just click OK.
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
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
we're coming to this main activity, so what we're gonna do here is to first i'm gonna delete this textview.
and now we're gonna put a button in here, so i'm just gonna drag this button into the activity.
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,
that you can actually come in and customize this id which is how you reference the control throw out your project,
but for right now we're gonna leave it at the default.
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.
so i'm gonna go to my main.java file. and under where i set the content view,
the first thing i need to do is to get a reference to that button control that i put in the xml.
so to do that i'm gonna create a button property, and i'm just gonna call it b, to keep it simple.
and in the way which we can get a reference to UI components in our xml is to use the findViewById method,
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,
we're gonna wanna go to R.id there you can see our button control with that name that was given by default,
so this will now return a reference and save it in this b property that we created,
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,
so we're basically casting it to a button control, so we now have our reference to that button stored in this b property.
so the next thing we need to is to set up the onClick listener, so that when ever the user clicks that button,
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,
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
is to create a new onClickListener so we're gonna say new, and i'll just start typing it,
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,
so when i click this we can see it's automatically created that stop for me, again if i hit ctrl+1
i'll be able to get some quick fixes here and i just wanna import the onClickListener class,
and i'll come down here, and put a semicolon. so now inside of this onClick method,
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
so the way in which we start activities, is to actually call the startActivity method,
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,
but this actually just broadcast, hey i want to activate this particular component or this activity.
so we're just gonna do an inline here, i'm gonna create a new intent.
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,
in which we pass in a context and then we pass in which class which represents the activity do we want to call?
so now when it comes to context, this is something that you'll have to use quite a bit as you doing android development.
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.
so now there's a few different ways to get the context, but because we're in an anonymous inner method here,
the easiest way to get the content is to just say Main.this
which is referencing our main file here, and this basically tells the system that hey the main activity is requesting this action.
so the next thing we send in is the class of which activity we want to launch, well that is Second
and then we give it the class property, and then we can close that.
so really quick just to recap, we've gotten a reference to our button which we define in our xml here,
we've set up the onClickListener, we just did it as an anonymous inner type event handler here,
there are other ways to it as well, which we'll cover when we get to talking more about button controls,
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.
we're just gonna start a new activity we're passing in this intent object, which basically is saying who is requesting this,
well it's this activity right here which is Main. and then which activity we'll want to launch.
so let's go ahead and launch that. and we'll save these resources.
and let's go to the emulator.
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,
and it says Sorry the application Activities has stopped unexpectedly.
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?
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.
if we click this and then go to other. we can see that there's one specific to android called DDMS.
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.
one of the most useful areas is LogCat so as your applications are running in the emulator or a device,
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.
so if we scroll up here we can see what happen, FATAL EXCEPTION : main ActivityNotFoundException.
so it couldn't find that activity that we created, so why was that?
it's because we haven't actually added it to the manifest file. so let's go back to the manifest file,
remember any activity that exists in your application, it needs to be defined here in the manifest file.
so all i'm gonna do is to create a new activity here.
and i'm just gonna start typing name and then hit ctrl+space give me the code hinting,
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,
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.
that's all we need to do with this point just to define this activity.
OK so let's go ahead and relaunch this and go back to our emulator.
and let this load-up.
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?
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
which basically tells the activity which xml file to use for it's visual content.
so what we're gonna do is to call setContentView and then give the id of the layout file that we wan to use.
so to get that we say R. and then go to layout and we wanna use the second.xml file here.
and with that we should be done. so let's launch it one last time.
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.
so again that's what it takes to add a new activity to your application. again you're gonna be creating a java file,
if your gonna be using xml for the UI, you're gonna need to create that.
and then a really important part is that you need to define it in your manifest file.
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.
AndroidApp.blog.ir