A new Calendar API is introduced in Android 4 supports applications that are designed
to read and write data to calendar provider.
By using this API, you will get list of calendars and respective events like calendar
from your phone, its created events and any calendars and events associated with
online (Gmail) accounts.
By using this API, you can also create, update and delete an event. For example,
when you create an event with this API, you can see this created event in your
phone calendar means whatever the operations you do with this API will be in
sync with the Calendars associated with your phone.
Please follow below steps to create sample Calendar app.
1) Once you create an Android Project from Visual Studio, Select project properties,
select Android Manifest and Check the READ_CALENDAR and WRITE_CALENDAR from required
permissions.
2) Once you set the above permissions, you can interact with calendar data
with CalendarContract class. For querying calendars you can use ContentUri as
below.
var calendarsUri = CalendarContract.Calendars.ContentUri;
3) Use CalendarContract to get required Calendar Columns, call as projection. Following
code includes the Calendar ID, Display Name and Account Name.
string[] calendarsProjection = {
CalendarContract.Calendars.InterfaceConsts.Id,
CalendarContract.Calendars.InterfaceConsts.CalendarDisplayName,
CalendarContract.Calendars.InterfaceConsts.AccountName
};
4) Use ManagedQuery method to return a cursor with calendar data
var cursor = ManagedQuery (calendarsUri, calendarsProjection, null, null, null);
5) The UI for this example contains a ListView to show list of calendars.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@android:id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
6) Also, specify the UI for each list item, which we place in a separate .axml file
as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/calDisplayName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dip" />
<TextView
android:id="@+id/calAccountName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12dip" />
</LinearLayout>
7) Below code to bind data from the cursor to UI using SimpleCursorAdapter as follows.
string[] sourceColumns = {CalendarContract.Calendars.InterfaceConsts.CalendarDisplayName,
CalendarContract.Calendars.InterfaceConsts.AccountName};
int[] targetResources = {Resource.Id.calDisplayName, Resource.Id.calAccountName};
SimpleCursorAdapter adapter = new SimpleCursorAdapter (this, Resource.Layout.CalListItem,
cursor, sourceColumns, targetResources);
ListAdapter = adapter;
8) When clicks on each Calendar list item, navigates to Events Activity to show list
of available events for the selected calendar.
ListView.ItemClick += (sender, e) => {
int i = (e as Android.Widget.AdapterView.ItemClickEventArgs).Position;
cursor.MoveToPosition(i);
int calId = cursor.GetInt (cursor.GetColumnIndex (calendarsProjection
[0]));
var showEvents = new Intent(this, typeof(EventListActivity));
showEvents.PutExtra("calId", calId);
StartActivity(showEvents);
};
From above piece of code, I am passing the selected Calendar ID and will be retried
in Events Activity OnCreate method as below.
_calId = Intent.GetIntExtra("calId", -1);
9) Use ManagedQuery as discussed above to get the list of events. Below piece of
code to retrieve calendar events and Bind to UI ListView with SimpleCursorAdapter.
var eventsUri = CalendarContract.Events.ContentUri;
string[] eventsProjection = {
CalendarContract.Events.InterfaceConsts.Id,
CalendarContract.Events.InterfaceConsts.Title,
CalendarContract.Events.InterfaceConsts.Dtstart
};
var cursor = ManagedQuery(eventsUri, eventsProjection,
String.Format("calendar_id={0}", _calId), null, "dtstart ASC");
string[] sourceColumns = {
CalendarContract.Events.InterfaceConsts.Title,
CalendarContract.Events.InterfaceConsts.Dtstart
};
int[] targetResources = { Resource.Id.eventTitle, Resource.Id.eventStartDate
};
var adapter = new SimpleCursorAdapter(this, Resource.Layout.EventListItem,
cursor, sourceColumns, targetResources);
ListAdapter = adapter;
Adding a Calendar Event:
1. Create a ContentValues instance.
2. Use keys from the CalendarContract.Events.InterfaceConsts class to populate the
ContentValues instance.
3. Use a ContentResolver to insert the event data into the calendar.
ContentValues eventValues = new ContentValues();
eventValues.Put(CalendarContract.Events.InterfaceConsts.CalendarId, _calId);
eventValues.Put(CalendarContract.Events.InterfaceConsts.Title, "Test Event from M4A");
eventValues.Put(CalendarContract.Events.InterfaceConsts.Description, "This is an event created from Mono for Android");
eventValues.Put(CalendarContract.Events.InterfaceConsts.Dtstart, GetDateTimeMS(2014, 12, 15, 10,
0));
eventValues.Put(CalendarContract.Events.InterfaceConsts.Dtend, GetDateTimeMS(2014, 12, 15, 11, 0));
eventValues.Put(CalendarContract.Events.InterfaceConsts.EventTimezone, "UTC");
eventValues.Put(CalendarContract.Events.InterfaceConsts.EventEndTimezone, "UTC");
var uri = ContentResolver.Insert(CalendarContract.Events.ContentUri,
eventValues);
Updating Calendar Event:
For updating you need a ContentValues object containing those elements that you want
to change. Use ContentResolver to update.
ContentValues values = new ContentValues();
values.Put(CalendarContract.Events.InterfaceConsts.Title, "Update Event from M4A");
values.Put(CalendarContract.Events.InterfaceConsts.Description, "This is an event updated from Mono for Android");
String[] selArgs =
new String[] { Java.Lang.Long.ToString(selectedEventId) };
int updated =
ContentResolver.
Update(
CalendarContract.Events.ContentUri,
values,
CalendarContract.Events.InterfaceConsts.Id + " =? ",
selArgs);
From above, selectedEventId is the ID retrieved when event is selected, sample piece
of code as below.
ListView.ItemClick += (sender, e) =>
{
int i = (e as Android.Widget.AdapterView.ItemClickEventArgs).Position;
cursor.MoveToPosition(i);
int eventId = cursor.GetInt(cursor.GetColumnIndex(eventsProjection[0]));
};
Deleting Calendar Event:
To delete you simply need the id of the event you want to delete. Use ContentResolver
to delete.
String[] selArgs = new String[] { Java.Lang.Long.ToString(selectedEventId) };
int deleted =
ContentResolver.
Delete(
CalendarContract.Events.ContentUri,
CalendarContract.Events.InterfaceConsts.Id + " =? ",
selArgs);
Adding Reminder to an Event:
ContentValues eventValues = new ContentValues();
eventValues.Put(CalendarContract.Reminders.InterfaceConsts.EventId, selectedEventId);
eventValues.Put(CalendarContract.Reminders.InterfaceConsts.Method, "Alert");
eventValues.Put(CalendarContract.Reminders.InterfaceConsts.Minutes, 10);
var uri = ContentResolver.Insert(CalendarContract.Reminders.ContentUri,
eventValues);
Adding Attendies to an Event:
ContentValues eventValues = new ContentValues();
eventValues.Put(CalendarContract.Attendees.InterfaceConsts.EventId, selectedEventId);
eventValues.Put(CalendarContract.Attendees.InterfaceConsts.AttendeeName, "Test1");
eventValues.Put(CalendarContract.Attendees.InterfaceConsts.AttendeeEmail, "test1@gmail.com");
var uri = ContentResolver.Insert(CalendarContract.Attendees.ContentUri,
eventValues);
Please download the working sample from below link.
https://www.nullskull.com/FileUpload/-407123783_CalendarDemo.zip