2

I trigger the creation of a calendar event through the following piece of code:

Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setData(CalendarContract.Events.CONTENT_URI);
startActivity(intent);

But I would like to specifiy which calendar should be used for the event creation (i.e set the initial value of the calendar dropdown in the event creation screen).

I've tried to provide ACCOUNT_TYPE, ACCOUNT_NAME and CALENDAR_ID

intent.putExtra(CalendarContract.Events.ACCOUNT_TYPE, accountType);
intent.putExtra(CalendarContract.Events.ACCOUNT_NAME, accountName);
intent.putExtra(CalendarContract.Events.CALENDAR_ID, calendarId);

but it has no effect on the calendar dropdown initial value.

Is it possible to specify a calendar in the event creation intent?

sdabet
  • 18,360
  • 11
  • 89
  • 158
  • Maybe you need to set `ACCOUNT_TYPE` too? – kichik Mar 21 '16 at 19:39
  • isn't it CalendarContract.Calendars.ACCOUNT_NAME ? – Amit K. Saha Mar 21 '16 at 19:45
  • @kichik I've tried `ACCOUNT_TYPE`, `ACCOUNT_NAME` and `CALENDAR_ID`, but it has no effect – sdabet Mar 22 '16 at 10:00
  • @AmitK.Saha it wouldn't change anything: they both have the same value. – sdabet Mar 22 '16 at 10:01
  • Try this. May it will help you. http://stackoverflow.com/questions/7859005/how-to-read-and-edit-android-calendar-events-using-the-new-android-4-0-ice-cream?answertab=active#tab-top – Joy Helina Mar 22 '16 at 11:01
  • @JoyHelina In this answer the calendar is not provided in the intent. – sdabet Mar 22 '16 at 11:06
  • 1
    Which calendar app did you try? Looking at [EditEventFragment.java](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android-apps/5.1.1_r1/com/android/calendar/event/EditEventFragment.java#327) I'd say it should work with the AOSP calendar app. However, many vendors (including Google) have their own proprietary calendar app which may not support this. The CalendarContact doesn't specify provisioning of the calendar id. – Marten Mar 22 '16 at 16:22
  • @Marten I use the default calendar app on a Nexus 5 (with Android 6.0) – sdabet Mar 22 '16 at 16:47
  • Have you ever tried that in the emulator? I'm curious if it works with the open source calendar app. – Marten Mar 22 '16 at 20:06

3 Answers3

5

I believe, there is no reliable way to achieve this with just an Intent. You may come across all sorts of calendar apps that may or may not support this.

If you don't mind if the user can not change the calendar you can achieve this with a simple trick:

Just create a "placeholder" event in the calendar you want to (like outlined in the Android Developer Guide) and fire an Intent.ACTION_EDIT Intent for this event instead.

Here is some (untested) code to show how this could work:

ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();

// provide an initial time
long now=System.currentTimeMillis();
values.put(Events.DTSTART, now);
values.put(Events.DTEND, now+3600000);
values.put(Events.TITLE, "");
values.put(Events.CALENDAR_ID, calendarId);
values.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());

// create the "placeholder" event
Uri uri = cr.insert(Events.CONTENT_URI, values);

// get the event ID that is the last element in the Uri
long eventId = Long.parseLong(uri.getLastPathSegment());

// launch the editor to edit the "placeholder" event
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setData(ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventId));
startActivityForResult(intent, 0);

The edit Intent probably doesn't return a status that tells you if the event has been saved or if the user dismissed the editor without saving. So you should read back the event and check if it has been modified by the user and delete it if it has not been changed.

Marten
  • 3,802
  • 1
  • 17
  • 26
  • How can we check whether the user has modified the event or not? – Pravinsingh Waghela Jun 01 '16 at 07:30
  • 1
    You know the event id, just load the event and compare the values to the ones you passed to the insert operation. Be aware that the user can delete the event or move it to another calendar (some calendar apps support that), so you should be prepared for the case that your placeholder event no longer exists (or has a different id). – Marten Jun 01 '16 at 10:17
  • Has anyone tried this workaround? The event is created as expected, and I'm opening the URI with the created event's ID, but the edit page doesn't include any of the created event's data, only new data I add to the Intent, so I think it's just defaulting to a create instead of edit. Maybe I need to wait a while for the event to propagate? Anything else I could be doing wrong? – grantpatterson Nov 01 '17 at 00:56
  • I've tested that a bit. The code above works as expected with "aCalendar". It works partially with the AOSP and stock HTC calendar apps. In these cases the title, description and location are correctly shown, but the date & time fields are not. In these cases it can be "fixed" by adding `EXTRA_EVENT_BEGIN_TIME` and `EXTRA_EVENT_END_TIME` to the edit intent. It doesn't seem to work with Google's Calendar App. – Marten Nov 02 '17 at 12:14
1

Its's Simple to add Event Using this simple Method.

public void onAddEventClicked(View view) {
        Intent intent = new Intent(Intent.ACTION_INSERT);
        intent.setType("vnd.android.cursor.item/event");

        Calendar cal = Calendar.getInstance();
        long startTime = cal.getTimeInMillis();
        long endTime = cal.getTimeInMillis() + 60 * 60 * 1000;

        intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startTime); 
        intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime);

        intent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true);

        //This is Information about Calender Event.
        intent.putExtra(Events.TITLE, "Siddharth Birthday");
        intent.putExtra(Events.DESCRIPTION, "This is a description");
        intent.putExtra(Events.EVENT_LOCATION, "My Guest House");
        intent.putExtra(Events.RRULE, "FREQ=YEARLY");

        startActivity(intent);
    }
0

Use the following code snippet to invoke the default calendar event.

Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
intent.setType("vnd.android.cursor.item/event");
startActivity(intent);

The following code bundle can be used to pass the information to calendar intent.

intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startTime);
intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,endTime);
intent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true);
intent.putExtra(Events.TITLE, "Neel Birthday");
intent.putExtra(Events.DESCRIPTION, "This is a sample description");
intent.putExtra(Events.EVENT_LOCATION, "My Guest House");
intent.putExtra(Events.RRULE, "FREQ=YEARLY");

May it will help you :) Thanks.

Source: How to Add an Event to Device Calendar in Android

You can check this one too. Adding Events to the User’s Calendar

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437