Eclipsed4utoo's Blog
Not Your Ordinary Programmer

Android – Open Contacts Activity and Return Chosen Contact

I was looking for a way to open the Contacts Activity, chose a Contact, then have that Contact returned to my application.  I found a number of code snippets, but they all used depreciated API calls.  So I decided to make this post with the updated API calls for Android 2.0.

First, you will need to add this permission to your AndroidManifest.xml file:

<uses-permission android:name="android.permission.READ_CONTACTS"/>

Next, add this constant as a class level variable:

private static final int PICK_CONTACT = 3;

Now, you will add the code to open the Contacts Activity.  This is done by using an Intent:

// I did this from a button click
public void btnAddContacts_Click(View view){
     Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
     startActivityForResult(intent, PICK_CONTACT);
}

Now, you will override the “onActivityResult” activity method, and get the Contact information:

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data){
    super.onActivityResult(reqCode, resultCode, data);

    switch(reqCode){
       case (PICK_CONTACT):
         if (resultCode == Activity.RESULT_OK){
             Uri contactData = data.getData();
             Cursor c = managedQuery(contactData, null, null, null, null);

             if (c.moveToFirst()){
                 // other data is available for the Contact.  I have decided
                 //    to only get the name of the Contact.
                 String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                 Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();
             }
         }
    }
}

And that is all you need to do.

6 Responses to “Android – Open Contacts Activity and Return Chosen Contact” »

  1. nka Says:

    Hi. What do you mean by “…have that Contact returned to my application.”?

  2. Ryan Alford Says:

    In the code above, it will open the “Contacts” application where a user can select a Contact. When the selection is made, the “Contacts” application will close and return an intent containing data about the Contact that was chosen. In my code above, I get the name of the Contact that was chosen and display it in a Toast.

    This is all done from my application. So on the click of a button in my application, I will open the Contacts application, the user will select a Contact, and I will be able to retrieve the Contact that the user chose and use that information in my application.

  3. Daf Says:

    Hi. How do I retrieve the phone number?

  4. Ryan Alford Says:

    @Daf,

    Here is a link to a good example of getting the phone number of a specific contact.

    http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/

    The phone numbers are stored in a different table than the other contact information. Therefore, you are going to have to make a separate query to get those.

  5. Daf Says:

    @Ryan

    Thanks!

  6. Gay Says:

    eclipsed4utoo.com, hoq do you do it?

Leave a Comment

Get Adobe Flash playerPlugin by wpburn.com wordpress themes