>> MORRILL: Hi. I'm Dan Morrill with Google developer programs. I bet by now you've seen
the application demos that we've posted online, and I hope you're as excited by them as we
are. But if you are a developer, you know there's more to it than just a fancy demo.
It has to be easy to build those applications as well. And that's what I'm here today to
show you. I'm going to build an application for Android using nothing but the tools that
are included in our SDK. Let's get started. The first thing we have to do is create a
new project. See, I'm using Eclipse, and to create the project, I just go to file, project.
And the project I'm going to create is an Android project. The project name can be whatever
you want. In this case I'm going to call it, HelloAndroid. The package name, I am today
entering as com.google.android.hello, but of course that's in our company's name space
and you should feel free to use whatever name space is appropriate for your organization.
The activity name is going to be HelloAndroid as well. The application name is the human
readable name of your application and this is what will appear in the title bar and window
and so on. So once we've entered that data, we go ahead and click finish and there's our
application. What did this create? Well, if you look in the tree over here, you'll see
that we have a source directory, including the file, HelloAndroid.java. This is the entry
point file to the application. This is the class that Android runs when you start the
application. Let's build an application that knows how to dial the phone. Specifically,
let's build an application that lists everyone in my contact list, lets me scroll around,
choose one, and then when I select it, it goes ahead and dials that phone number. Let's
see how that's built. The first thing that we want to do is check out this file over
here called main.xml in the layout directory. This file is what we refer to as an XML-based
layout. So, in this file, as you can see right now, it has only a single text view that just
displays some Hello World text. So the first thing we want to do is change that text so
that it no longer says, "Hello World" and instead has this label instead called Name.
Then we're going to copy that, because we want two of these. And then once we've copied
that, we're going to move down a little bit and paste it and now we've got two copies
of it. For the second copy, we're going to remove this text because we don't want it
to have fixed text. In this case, we want it to have an ID instead because we're going
to refer to the ID from our source code. And so there we are. Now, we've got our XML-based
layout file. Well, first thing that we need to do is come up here to the HelloAndroid
class. Currently, it implements or extends the class activity. We actually need it to
extend a different class called ListActivity. ListActivity is a convenient subclass that
really just says, "I'm going to display a list or any information." And it's got a whole
bunch of useful methods that we can take advantage of for this application so that we don't have
to write them ourselves. The first thing that we're going to do is get access to some data.
The Android way of doing that is to use what's known as a content provider. We get access
to the content provider system via this method call here. And what that does is it lets us
issue a query. In this case, we're querying the system for the entire list of contacts
that are available on the phone. In response, we get a Cursor. The Cursor is what we use
to actually access specific data from the query that we requested. Now, we have to tell
the UI exactly what we want to display because, of course, the contact list consists of a
lot more than just, say, first name and the phone number. It might also have an email
address or a mobile phone number and a home phone number and so on. So what we're going
to do is create a mapping. And the mapping tells us which column or name in the content
provider that we want to map to which UI element. And so, in this specific case, we want to
say "people.name," which is to say the person's name gets mapped to the entry that we called
row entry in our XML layout. And so just to refresh your memory, I'll switch back to that
file and show you right here. You can see that we have an ID that we created and called
row entry. And then, we call this method setListAdapter and the ListAdapter itself is an instance
of another class called SimpleCursorAdapter and that's a class that really has all the
smarts and all the logic in it to make use of the mapping that we just declared. So to
summarize this method real quick, first, we get the data. Then we map the data from the
data source to the user interface. Then we create a class that knows how to actually
accomplish that mapping and then we set that to be our current adapter. Once we've done
that, that will display things, you know, it will show us a list of all our contacts.
But what it won't do is it won't let us click on any of them. To click on them we have to
come over here and do one last thing. We need to go to the source and then override a method.
The method that we want to override is down here towards the bottom and it's called onListItemClick.
Click OK and there, we've got a method body. What this method does is, this is the method
that's called every time the user actually clicks something in the user interface. In
this specific case, we want it to dial the phone. And so the first thing that we need
to do is create a class called Intent. And an Intent is really almost kind of a generic
concept. What it means is it's a reflection of an application's "intention" to perform
some activity. So I create an Intent object and I say that the specific activity that
I want it to perform is call_action, which means dial the phone. Obviously, I need to
tell it what number to dial and so I got to the Cursor that I got from the content provider.
I query the adapter for the specific row that the user clicked on. I extract a little bit
of data from it. I set that data on the Intents so that now the Intent knows what it's suppose
to do, and then, finally, I call this method startActivity. So, when we call startActivity,
that's what will actually cause the dialer to appear on the screen and initiate the phone
call. But certain operations that the device supports, such as dialing the phone and accessing
the users contact list, are protected and the user needs to grant permission before
an application can access them. And so we have to request that permission via our Android
manifest. So if we come over here and double click on this Android manifest file, you'll
see that there's already in here a definition of what the contents of our application are,
which is what Android manifest does. It's sort of the traffic cop of your application.
It specifies its contents as well as what it does. So what we need to do is add two
lines to this file. The first line that we need to add is a request for a permission
called read_contacts. This permission, if the user chooses to grant it, will allow the
application to access the user's contact list. Similarly, we also need to create or request
a permission called call_phone. And what that will do is request that the user allow this
application to dial a phone. Now that we've done that, we go ahead and save that file,
and if we come back up here and run the application again, then, hey, there we are. As you can
see, it contains all the entries in my contact list and hey, let's dial my wife. Well, it's
an emulator so it's not actually going to do anything, but that is how it works. So
this is a surprisingly sophisticated example for the number of lines of code that we've
done. And throughout the entire application framework, we've really tried to make it this
easy to use. And I think that as, you know, explore Android, you'll find it to be very
easy and pleasant to develop for. I hope that you've enjoyed this and I hope that you think
this was time well spent. I thank you for your time and I hope to see you online.