Android Android – Open Contacts Activity and Return Chosen Contact

15 Comments

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.

15 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?

  7. Shailendra Says:

    This code is not working inside GroupActivity and showing LogCat status

    09-02 12:26:52.846: WARN/ActivityManager(64): startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { act=android.intent.action.PICK dat=content://com.android.contacts/contacts cmp=com.android.contacts/.ContactsListActivity }

    Any suggetion?

  8. Sayani Says:

    Thanks
    It’s working perfectly.

  9. nu_developer Says:

    This list of Contacts that comes up only lets u pick a contact. Is there any way I can also allow the user to create a new contact while displaying this Contact List. Or is there no way to do that. Also, suppose my user simply wants to send an sms to a person who is not there in his Contact list. So, is there a way i can put a “None” button or item in the list so the user can proceed without selecting a contact from this list. Thanks for your time.

  10. anudeep Says:

    hello there,

    well in my application i need to access contacts based on a particular substring and store the retrieved contacts (all the contacts after the filtering process) into a string (only name and phone number).. could you show me a way as to how i can achieve this??

  11. Resident Evil Says:

    granda ciona de antosula y entra con aratirio aeriameg. milóis a dindesumo y prertisti gancosi con codondo caspetr!

  12. Jule Atala Says:

    I love your blog.. very nice colors & theme. Did you create this website yourself or did you hire someone to do it for you? Plz respond as I’m looking to create my own blog and would like to know where u got this from. kudos

  13. Unichleah Says:

    buy best online online shopping

  14. munib Says:

    Nyc man ..love it … code 100% working …

  15. ashish Says:

    could you do same for the call logs, I mean select a contact from call log activity

Leave a Reply