Android – Create new Contact Programmatically

Step 1:

Add permission in Manifest.xml

<uses-permission android:name="android.permission.WRITE_CONTACTS" />
Step 2:
In java code

public void WritePhoneContact(String displayName, String number, Context cntx /*App or Activity Ctx*/) {
    Context contetx = cntx; //Application's context or Activity's context
    String strDisplayName = displayName; // Name of the Person to add
    String strNumber = number; //number of the person to add with the Contact
    System.out.println("NAME ====> " + strDisplayName + "    NUMBER ====>  " + strNumber);
    ArrayList<ContentProviderOperation> cntProOper = new ArrayList<ContentProviderOperation>();
    int contactIndex = cntProOper.size();//ContactSize

    //Newly Inserted contact
    // A raw contact will be inserted ContactsContract.RawContacts table in contacts database.
            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null).build());

    //Display name will be inserted in ContactsContract.Data table
            .withValueBackReference(ContactsContract.RawContacts.Data.RAW_CONTACT_ID, contactIndex)
            .withValue(ContactsContract.RawContacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, strDisplayName) // Name of the contact
    //Mobile number will be inserted in ContactsContract.Data table
    cntProOper.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)//Step 3
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, contactIndex)
            .withValue(ContactsContract.RawContacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, strNumber) // Number to be added
            .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE).build()); //Type like HOME, MOBILE etc
    try {
        // We will do batch operation to insert all above data
        //Contains the output of the app of a ContentProviderOperation.
        //It is sure to have exactly one of uri or count set
        ContentProviderResult[] contentProresult = null;
        contentProresult = contetx.getContentResolver().applyBatch(ContactsContract.AUTHORITY, cntProOper); //apply above data insertion into contacts list
    } catch (RemoteException exp) {
    } catch (OperationApplicationException exp) {

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s