How to Create an Address Book in the Google Android Programming Environment – Finishing the Application

Page content

In our last articles we were developing an Address Book via Google Android. In the first part we analyzed how we were going to start creating the application (Analysis is very important when we are developing applications), in the second part we started developing the “most difficult” part of the application: The ListActivity and how to handle the Content Providers. Now, lets finish our application by having a look to some other Listeners we can use in the ListActivity and drawing the contact Screen with the “Call” and “Send SMS” buttons and its Intents. Lets Start!

Listeners - Part II

In the Article How-to create an Address Book in the Google Android Programming Environment - ListActivity Implementation we had a look to the onItelSelected Listener. With this listener we can find out with item was SELECTED (testing with a real device or a emulator, it works when you move the arrow buttons thought the elements of the list and you push the “enter” button). This could be fine if we use the buttons very often, but its more instinctive to use the touch screen. If we want to click in a element of the list, and then launch a listener from this action, we need to use the

onListItemClick(ListView l, View v, int position, long id)

Every time the user clicks on an element of the list, this method will be called.

l The ListView where the click happened

v The view that was clicked within the ListView

positionn The position of the view in the list

id The row id of the item that was clicked

(Ref: Google)

So here, inside this method we can place the same piece of code that we used in the other Listener.

Contact Page

This class will extend from an Activity. We need to create a .xml file with the appearance of the page (how contact information is going to be displayed). This will be left to your imagination, we know how to do this with Droid Draw, so there is not much difficulty. The important thing in this page is:

-How to get the information from the Intent

-How to set the contact information to the UI

If we go back, in my last article, I wrote about Intents and how to send information from one Activity to another. In that case, we created an Intent, and we sent the “User name” to the Contact Page Activity. Its time to get this information in the new Activity that has been created.

In the ContactPage Activity, we need to do the following:

Intent intent = getIntent().

This will get the Intent we sent from the ContactList ListActivity, and with the following line, we retrieve the information from the Intent.

_String userName = intent.getStringExtra("_USER_NAME “);

(We are using the same string we used in the ListActivity: USER_NAME)

Now, in userName we have the name of the user we have clicked on the ListView (we could have retrieved the User ID, or the user Phone…)

Now we need to get the following example elements from the .xml file.

nameView = (TextView) findViewById(;

imageView = (ImageView) findViewById(;

callButton = (ImageButton) findViewById(;

smsButton = (ImageButton) findViewById(;

Lets suppose that we have created an TextView with the “name” id, a ImageView with the id “picture” and two ImageButtons with the followings ids: “call” and “sms”. Yo can add whatever information you want, this is just an example to show how to deal with them.

Now, we have the the User Name, we can get the full information, getting them from the Content Provider. Lets do it again, once more.

Cursor c = getContentResolver().query(People.CONTENT_URI, null, People.NAME +”=’” +userName+”’”, null, null);

Select * from people where name = “Pepe Botijo”

(To people who have never deal with SQL, this is the language its used to manage Data Bases)

Once we get the cursor, we move trough it

if (c.moveToFirst()) {

idColumn = c.getColumnIndex(People._ID);

nameColumn = c.getColumnIndex(People.NAME);

phoneColum = c.getColumnIndex(People.NUMBER);

do {

String id = c.getString(idColumn);

String name = c.getString(nameColumn);

String phoneNumber = c.getString(phoneColumn);

Uri peopleURI = ContentUris.withAppendedId(People.CONTENT_URI, new Long(id));

Bitmap contactPicture = People.loadContactPhoto(this,peopleURI, R.drawable.default_image, null); imageView.setImageBitmap(contactPicture);


} while (c.moveToNext());


Lets explain a bit this code.

Once we have the cursor, we move through it to retrieve the information we want. To get the picture of the contact, we need to use a special method: People.loadContactPhoto. And give this method the URI of the contact we want to get the picture (Uri appended with the witAppendedID method and the ID of the contact). To finish, we set the values we have retrieved to the view, adding the texts and the image.


Its time to create the listeners for every Button we have created.

In the call Button, we create an Intent, with the “ACTION_CALL” (to make a call) and we set the phone number with “setData”

callButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

Intent i = new Intent();


i.setData(Uri.parse(“tel:” + phoneNumber));




The SMS button is quite similar, the Action in this case is “ACTION_SENTDO” and we set the phone number.

smsButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

Intent i = new Intent();


i.setData(Uri.parse(“smsto:” + phoneNumber));




Want more?

In our next artcile I will show more application examples. Stay close!

This post is part of the series: How to- Develop Applications on Android: Simple Applications

In this 5-articles series we will see how to create simple applications, using the tools and knowledge we have acquiredfrom others articles.

  1. How to Create a Calculator in Google Android: Part I
  2. Guide to Creating a Calculator in Android
  3. Dev Guide to Creating an Android Address Book
  4. How-to create an Address Book in the Google Android Programming Environment - ListActivity Implementation
  5. Dev Guide to Making an Android Address Book