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're 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 STK.
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 hello android.
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 hello android 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 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 world text.
So the first thing that 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 it, we're going to move down a
little bit and paste it.
And now we have 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 have our XML based layout file.
Well, first thing that we need to do is come up here to the
hello android class.
Currently it implements, or extends, the class activity.
We actually need it to extend a different
class called list activity.
List activity is a convenient sub class that really just
says I'm going to display list oriented information.
And it has a whole bunch of useful methods that we can
take advantage of for this applications 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
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 used 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, a first name and a 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 set list adapter, and the list
adapter itself is an instance of another class called simple
cursor adapter, 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, in other
words 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 an override a method.
The method that we want to override is down here towards
the bottom and it's called on list item click.
Click OK, and there we have 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 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
go 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 intent so that now the intent knows what it's
supposed to do, and then finally I call this method
start activity.
So when we call start activity 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 user's 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 here and double click on this android manifest
file, you'll see that there is already in here a definition
of what the contents of our application are, which is what
android manifest does.
It's 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 the phone.
So 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 there we are.
As you can see, it contains all of the entries in my
contact list and 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 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.