Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Adding Calendar Events

Author: Ian Darwin
Published? false -- FormatLanguage: W

Problem:

You want to add an event to the user's on-device calendar.

Solution:

On any release you can use an Intent to invoke the user's chosen Calendar application to create the event.

On any 2.x or 3.x release, you can non-portably use a Content Provider, but you have to hard-code the string names of the fields.

On Ice Cream Sandwich (Android 4.0, API level 14) there is a CalendarContract class which provide the names of the fields to use with the Content Provider.

Discussion:

You can do almost anything with Intents in Android, including requesting the creation of Calendar events. Assuming the user has a calendaring application installed (there is a default on all standard devices, and on the emulator in modern releases), that application will start up, usually in its "Add Event" dialog. The intent must be of type ACTION_EDIT, have a type of "vnd.android.cursor.item/event", and have at least a begin time and end time and a title. All of the other calendar fields, such as recurrence rules, description, location, may be specified, but you have to do some digging, or trial-and-error, to find out the names to use for these. The code is shown here:

public void addEvent(Context ctx, String title, Calendar start, Calendar end) {
    Log.d(TAG, "AddUsingIntent.addEvent()");
    Intent intent = new Intent(Intent.ACTION_EDIT);
    intent.setType("vnd.android.cursor.item/event");
    intent.putExtra("title", title);
    intent.putExtra("beginTime", start.getTimeInMillis());
    intent.putExtra("endTime", end.getTimeInMillis());
    intent.putExtra("allDay", false);
    ctx.startActivity(intent);
}

The ContentProvider-based method may be preferable if you do not want the user to have to interact with a calendaring application. In Froyo and Gingerbread and Honeycomb releases, you had to "know" the names to use for the various fields you wanted to interact with. We do not cover this method as it is officially unsupported, but you can find a good article on the web by our contributor Jim Blacker, at [1].

Effective with Ice Cream Sandwich (Android 4, API level 14), the new CalendarContract class holds, in a variety of nested classes, all the constants needed to make a portable calendar application. This is shown inserting a Calendar Event directly into the user's first Calendar (using id 1); obviously there should be a drop-down listing the user's calendars in a real application.

public void addEvent(Context ctx, String title, Calendar start, Calendar end) {
    Log.d(TAG, "AddUsingContentProvider.addEvent()");
        
    TextView calendarList = 
        (TextView) ((Activity) ctx).findViewById(R.id.calendarList);
        
    ContentResolver contentResolver = ctx.getContentResolver();
        
    ContentValues calEvent = new ContentValues();
    calEvent.put(CalendarContract.Events.CALENDAR_ID, 1); // XXX pick)
    calEvent.put(CalendarContract.Events.TITLE, title);
    calEvent.put(CalendarContract.Events.DTSTART, start.getTimeInMillis());
    calEvent.put(CalendarContract.Events.DTEND, end.getTimeInMillis());
    calEvent.put(CalendarContract.Events.EVENT_TIMEZONE, "Canada/Eastern");
    Uri uri = contentResolver.insert(CalendarContract.Events.CONTENT_URI, calEvent);
        
    // The returned Uri contains the content-retriever URI for 
    // the newly-inserted event, including its id
    int id = Integer.parseInt(uri.getLastPathSegment());
    Toast.makeText(ctx, "Created Calendar Event " + id,
        Toast.LENGTH_SHORT).show();
}

Both these methods will add the event to the user's calendar. The user is expected to have set up at least one Calendar account. Unfortunately ICS does not ship with a Calendar Account Service for the popular CalDav/ICal/Vcal web service (including Apple Calendar), so only Google Calendar and Microsoft Exchange are supported out of the box. I usually remedy this by installing the CalDav provider from the Android Market; see [2] in a browser, or [market://search?q=org.dmfs.caldav.lib] on-device.

There is much more to both ways of doing it; please consult the official documentation for all the options.

Download:

The source code for this project is in the Android Cookbook repository at http://github.com/IanDarwin/Android-Cookbook-Examples, in the subdirectory CalAdder.
No records found.