Android Contacts – Invoke the Edit Contact Intent

Sometimes, when working with Android contacts, you will need a quick way of showing a contact page, including all the associated fields.
Android has the advantage of reusing almost any piece of code available and the system standard controls make no exception – so you can use the system contacts dialog to load/edit a pre-existing contact.

To open a contact using the Intent.ACTION_EDIT, we have a few variants, with different results:

  • – passing the contact_ID as a parameter with setData():
  • Uri uri = Uri.parse(iduri);
    i.setData(uri);
    Intent i = new Intent(Intent.ACTION_EDIT);
    i.setData(Uri.parse(ContactsContract.Contacts.CONTENT_LOOKUP_URI + "/" + id));
    startActivityForResult(i, idEDIT_CONTACT);
    

    works, but it has some issues on a few Android devices.

  • -passing the LOOKUP key as a parameter:
  • Intent i = new Intent(Intent.ACTION_EDIT);
    i.setData(Uri.parse(ContactsContract.Contacts.CONTENT_LOOKUP_URI + "/" + id));
    startActivityForResult(i, idEDIT_CONTACT);
    

    This works best, and in the case of aggregated contacts, will ask you to select the particular contact you want to open. Using startActivityForResult and onActivityResult, we get a notification when the system form has been closed (either by using Cancel or Save).

    Alternatively you can first search for a contact (using Name, phone number, etc) and then get the Contact_ID to open the Edit contact Intent . Here is the source code for this sample. It first creates a contact, then it opens it using the lookup key.
    AndroidContacts-3

  • Update April 07, 2011 Another alternative that seems to work even better:
  • Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, read_id); 
    i.setData(contactUri);
    startActivityForResult(i, idEDIT_CONTACT);
    

    Where read_id is a long containing the contact id.

    This article has 1 Comment

    Leave a Reply