Search code examples
androidandroid-contentproviderandroid-contacts

How to Add a contact with Date of Birth into device contacts programmatically?


I am using the following code for inserting a contact, but i am unable to find the Date_Of_Birth field to add it.

ContentValues values = new ContentValues();
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, "0123456789");
values.put(Phone.TYPE, Phone.TYPE_CUSTOM);
values.put(Phone.LABEL, "Ravi");
Uri dataUri = getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);

Solution

  • ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
                            .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE)
                            .withValue(ContactsContract.CommonDataKinds.Event.START_DATE, "26-05-2015")
                            .withValue(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY)
                            .build());
    

    As per my knowledge, we can add the birthday in the above format but only if the device supports. As lot of devices do not have birthday column in default device contacts. When you create a contact programatically its created on device, where the birthday field is not available. it later on gets synced with the google account and the birthday field gets visible. I am posting this as it may be useful for someone at sometime.