1

I use this code to simply add a contact:

    Intent intent = new Intent(ContactsContract.Intents.Insert.ACTION);
    intent.setType(ContactsContract.RawContacts.CONTENT_TYPE);

    intent.putExtra(ContactsContract.Intents.Insert.NAME, edNome.getText().toString() + ' ' + edCognome.getText().toString())
            .putExtra(ContactsContract.Intents.Insert.PHONE, edCellulare.getText().toString())
            .putExtra(ContactsContract.Intents.Insert.PHONE_TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);

    startActivity(intent);

I need to insert also birthday date. I saw there is the option "ContactsContract.CommonDataKinds.Event" but how do I add this to the intent?

Simone
  • 311
  • 4
  • 16

2 Answers2

1

You can add birthday like below code:

Here 1 is value of calendarId for birthday.

   private void addEvent(){

        ContentResolver cr = ((Activity)Forms.Context).ContentResolver;
        ContentValues values = new ContentValues();
        String eventUriString = "content://com.android.calendar/events";

//Insert Events in the calendar...
        values.Put(CalendarContract.Events.InterfaceConsts.CalendarId, 1);
        values.Put(CalendarContract.Events.InterfaceConsts.Title, title);
        values.Put(CalendarContract.Events.InterfaceConsts.Status, 1);
        values.Put(CalendarContract.Events.InterfaceConsts.Description, description);
        values.Put(CalendarContract.Events.InterfaceConsts.Dtstart, GetDateTimeMS(year, month, day, hour, minute));
        values.Put(CalendarContract.Events.InterfaceConsts.Dtend, GetDateTimeMS(year, month, day, hour, minute));
        values.Put(CalendarContract.Events.InterfaceConsts.AllDay, allday ? "1" : "0");
        values.Put(CalendarContract.Events.InterfaceConsts.HasAlarm, hasalarm ? "1" : "0");
        values.Put(CalendarContract.Events.InterfaceConsts.EventColor, Android.Graphics.Color.Green);
        values.Put(CalendarContract.Events.InterfaceConsts.EventTimezone, "GMT+" + zone + ":00");
        values.Put(CalendarContract.Events.InterfaceConsts.EventEndTimezone, "GMT+" + zone + ":00");
        cr.Insert(Android.Net.Uri.Parse(eventUriString), values);
    }
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
0

you can't modify a contact's birthday via intent, you must use ContactsContract APIs to modify contacts.

According to the docs at: https://developer.android.com/training/contacts-provider/modify-data#EditContact You can only edit via intent the list of attributes at: https://developer.android.com/reference/android/provider/ContactsContract.Intents.Insert Which contains data items like emails and phones, but birthdays or events are not on the list.

The closest thing is to just open the contact's edit screen and have the user add the birthday herself:

Intent editIntent = new Intent(Intent.ACTION_EDIT);
editIntent.setDataAndType(theContactUri, Contacts.CONTENT_ITEM_TYPE);
startIntent(editIntent);
marmor
  • 27,641
  • 11
  • 107
  • 150