ZOFTINO.COM android and web dev tutorials

How to Read and Write Calendar Data in Android

If your application functionality or feature needs user calendar information, your app can get the calendar data using calendar provider or calendar intents. Calendar provider is a content provider which is one of the components in android and lets app share data with other apps.

To know more about content providers, you can read content providers post.

App needs to have access permissions in order for it to access calendar provider. App can request required permissions in manifest file.

<uses-permission android:name="android.permission.WRITE_CALENDAR"></uses-permission>
<uses-permission android:name="android.permission.READ_CALENDAR"></uses-permission>

Read and write operations on calendar data can be performed using calendar intents also. These intents take user to calendar app to perform operations. This way, you don’t need to include calendar permissions in manifest file.

Calendar data is stored in relational database tables. Calendar data tables are calendar, event, instances, attendees and reminders. Calendar table stores information about different calendars, each row represents a type of calendar. Event table stores event information such as event start time, end time etc. Instances table contains event start and end time, multiple records exist in instances table for events that occur more than once. Attendees table contains event attendees. Reminders table contains alert information for events.

Adding Data to Calendar Table Using Calendar Provider API

Below example show how to add calendar data to Calendar table, this is required only when your building new local calendar app. Calendar data can only be added by sync adapter meaning you need to append CALLER_IS_SYNCADAPTER as true to Uri as shown below.

 public void addCalendar() {

    ContentValues contentValues = new ContentValues();
    contentValues.put(CalendarContract.Calendars.ACCOUNT_NAME, "cal@zoftino.com");
    contentValues.put(CalendarContract.Calendars.ACCOUNT_TYPE, "cal.zoftino.com");
    contentValues.put(CalendarContract.Calendars.NAME, "zoftino calendar");
    contentValues.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, "Zoftino.com Calendar");
    contentValues.put(CalendarContract.Calendars.CALENDAR_COLOR, "232323");
    contentValues.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER);
    contentValues.put(CalendarContract.Calendars.OWNER_ACCOUNT, "cal@zoftino.com");
    contentValues.put(CalendarContract.Calendars.ALLOWED_REMINDERS, "METHOD_ALERT, METHOD_EMAIL, METHOD_ALARM");
    contentValues.put(CalendarContract.Calendars.ALLOWED_ATTENDEE_TYPES, "TYPE_OPTIONAL, TYPE_REQUIRED, TYPE_RESOURCE");
    contentValues.put(CalendarContract.Calendars.ALLOWED_AVAILABILITY, "AVAILABILITY_BUSY, AVAILABILITY_FREE, AVAILABILITY_TENTATIVE");


    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CALENDAR}, MY_CAL_WRITE_REQ);
    }

    Uri uri = CalendarContract.Calendars.CONTENT_URI;
    uri = uri.buildUpon().appendQueryParameter(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER,"true")
            .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, "cal@zoftino.com")
            .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, "cal.zoftino.com").build();
    getContentResolver().insert(uri, contentValues);
}

Getting Data from Calendar Table Using Calendar Provider API

Content resolver query() method is used to get content provider data. You need to pass list of columns that need to be retrieved, where clause, where clause arguments and sort order to query() method.

 public void getDataFromCalendarTable(View v) {
    Cursor cur = null;
    ContentResolver cr = getContentResolver();

    String[] mProjection =
            {
                    CalendarContract.Calendars.ALLOWED_ATTENDEE_TYPES,
                    CalendarContract.Calendars.ACCOUNT_NAME,
                    CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
                    CalendarContract.Calendars.CALENDAR_LOCATION,
                    CalendarContract.Calendars.CALENDAR_TIME_ZONE
            };

    Uri uri = CalendarContract.Calendars.CONTENT_URI;
    String selection = "((" + CalendarContract.Calendars.ACCOUNT_NAME + " = ?) AND ("
            + CalendarContract.Calendars.ACCOUNT_TYPE + " = ?) AND ("
            + CalendarContract.Calendars.OWNER_ACCOUNT + " = ?))";
    String[] selectionArgs = new String[]{"cal@zoftino.com", "cal.zoftino.com",
            "cal@zoftino.com"};

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CALENDAR}, MY_CAL_REQ);
    }
    cur = cr.query(uri, mProjection, selection, selectionArgs, null);

    while (cur.moveToNext()) {
        String displayName = cur.getString(cur.getColumnIndex(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME));
        String accountName = cur.getString(cur.getColumnIndex(CalendarContract.Calendars.ACCOUNT_NAME));

        TextView tv1 =  new TextView(this);
        tv1.setText(displayName);
        cont.addView(tv1);
    }

}

Modifying Calendar Data Using Calendar Provider API

public void updateCalendar(View view) {

    ContentValues contentValues = new ContentValues();
    contentValues.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, "Zoftino.com GIGA Calendar");
    contentValues.put(CalendarContract.Calendars.NAME, "zoftino giga calendar");

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CALENDAR}, MY_CAL_WRITE_REQ);
    }

    Uri uri = CalendarContract.Calendars.CONTENT_URI;

    String mSelectionClause = CalendarContract.Calendars.ACCOUNT_NAME+ " = ? AND ";
    mSelectionClause = mSelectionClause + CalendarContract.Calendars.ACCOUNT_TYPE+ " = ?";
    String[] mSelectionArgs = {"cal@zoftino.com", "cal.zoftino.com"};

    int updCount = getContentResolver().update(uri, contentValues,mSelectionClause,mSelectionArgs);

}

Adding Data to Events Table Using Calendar Provider API

public void addEvent(View view) {

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CALENDAR}, MY_CAL_WRITE_REQ);
    }

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

    Calendar beginTime = Calendar.getInstance();
    beginTime.set(2017, 02, 04, 9, 30);

    Calendar endTime = Calendar.getInstance();
    endTime.set(2017, 4, 4, 7, 35);

    ContentValues values = new ContentValues();
    values.put(CalendarContract.Events.DTSTART, beginTime.getTimeInMillis());
    values.put(CalendarContract.Events.DTEND, endTime.getTimeInMillis());
    values.put(CalendarContract.Events.TITLE, "Tech Stores");
    values.put(CalendarContract.Events.DESCRIPTION, "Successful Startups");
    values.put(CalendarContract.Events.CALENDAR_ID, 2);
    values.put(CalendarContract.Events.EVENT_TIMEZONE, "Europe/London");
    values.put(CalendarContract.Events.EVENT_LOCATION, "London");
    values.put(CalendarContract.Events.GUESTS_CAN_INVITE_OTHERS, "1");
    values.put(CalendarContract.Events.GUESTS_CAN_SEE_GUESTS, "1");

    cr.insert(CalendarContract.Events.CONTENT_URI, values);
}

Getting Data from Events Table Using Calendar Provider API

 public void getDataFromEventTable(View v) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CALENDAR}, MY_CAL_REQ);
    }
    Cursor cur = null;
    ContentResolver cr = getContentResolver();

    String[] mProjection =
            {
                            "_id",
                            CalendarContract.Events.TITLE,
                            CalendarContract.Events.EVENT_LOCATION,
                            CalendarContract.Events.DTSTART,
                            CalendarContract.Events.DTEND,
            };

    Uri uri = CalendarContract.Events.CONTENT_URI;
    String selection = CalendarContract.Events.EVENT_LOCATION + " = ? ";
    String[] selectionArgs = new String[]{"London"};

    cur = cr.query(uri, mProjection, selection, selectionArgs, null);

    while (cur.moveToNext()) {
        String title = cur.getString(cur.getColumnIndex(CalendarContract.Events.TITLE));

        TextView tv1 =  new TextView(this);
        tv1.setText(title);
        cont.addView(tv1);
    }

}
 

Modifying Events Data Using Calendar Provider API

 public void updateEvent(View view) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CALENDAR}, MY_CAL_WRITE_REQ);
    }
    ContentValues contentValues = new ContentValues();
    contentValues.put(CalendarContract.Events.TITLE, "Zoftino.com Tech Event");
    contentValues.put(CalendarContract.Events.EVENT_LOCATION, "NEW YORK");

    Uri uri = CalendarContract.Events.CONTENT_URI;

    String mSelectionClause = CalendarContract.Events.TITLE+ " = ?";
    String[] mSelectionArgs = {"Tech Stores"};

    int updCount = getContentResolver().update(uri, contentValues,mSelectionClause,mSelectionArgs);

}
 

Deleting Events Data Using Calendar Provider API

 public void deleteEvent(View view) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CALENDAR}, MY_CAL_WRITE_REQ);
    }

    Uri uri = CalendarContract.Events.CONTENT_URI;

    String mSelectionClause = CalendarContract.Events.TITLE+ " = ?";
    String[] mSelectionArgs = {"Zoftino.com Tech Event"};

    int updCount = getContentResolver().delete(uri,mSelectionClause,mSelectionArgs);

}
 

Getting Data from Instances Table Using Calendar Provider API

 public void getDataFromInstancesTable(View v) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CALENDAR}, MY_CAL_REQ);
    }
    Cursor cur = null;
    ContentResolver cr = getContentResolver();

    String[] mProjection =
            {
                    CalendarContract.Instances.BEGIN,
                    CalendarContract.Instances.END,
            };

    Uri uri = CalendarContract.Instances.CONTENT_URI;
    String selection = CalendarContract.Instances.EVENT_ID + " = ? ";
    String[] selectionArgs = new String[]{"2"};

    cur = cr.query(uri, mProjection, selection, selectionArgs, null);

    while (cur.moveToNext()) {
        String begin = cur.getString(cur.getColumnIndex(CalendarContract.Instances.BEGIN));

        TextView tv1 =  new TextView(this);
        tv1.setText(begin);
        cont.addView(tv1);
    }

}
 

Adding Data to Instances Table Using Calendar Provider API

 public void addInstance(View view) {

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CALENDAR}, MY_CAL_WRITE_REQ);
    }

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

    Calendar beginTime = Calendar.getInstance();
    beginTime.set(2017, 02, 8, 11, 15);

    Calendar endTime = Calendar.getInstance();
    endTime.set(2017, 5, 8, 9, 45);

    ContentValues values = new ContentValues();
    values.put(CalendarContract.Instances.EVENT_ID, "2");
    values.put(CalendarContract.Instances.BEGIN, beginTime.getTimeInMillis());
    values.put(CalendarContract.Instances.END, endTime.getTimeInMillis());


    cr.insert(CalendarContract.Reminders.CONTENT_URI, values);
}
 

Modifying Instances Data Using Calendar Provider API

 public void updateInstance(View view) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CALENDAR}, MY_CAL_WRITE_REQ);
    }
    ContentValues contentValues = new ContentValues();
    Calendar endTime = Calendar.getInstance();
    endTime.set(2017, 8, 9, 9, 45);
    contentValues.put(CalendarContract.Instances.END, endTime.getTimeInMillis());

    Uri uri = CalendarContract.Instances.CONTENT_URI;

    String mSelectionClause = CalendarContract.Instances.EVENT_ID+ " = ? ";
    String[] mSelectionArgs = {"2"};

    int updCount = getContentResolver().update(uri, contentValues,mSelectionClause,mSelectionArgs);

}
 

Deleting Instances Data Using Calendar Provider API

 public void deleteIntance(View view) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CALENDAR}, MY_CAL_WRITE_REQ);
    }

    Uri uri = CalendarContract.Instances.CONTENT_URI;

    String mSelectionClause = CalendarContract.Instances.EVENT_ID+ " = ?";
    String[] mSelectionArgs = {"2"};

    int updCount = getContentResolver().delete(uri,mSelectionClause,mSelectionArgs);

}
 

Getting Data from Attendees Table Using Calendar Provider API

 public void getDataFromAttendeesTable(View v) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CALENDAR}, MY_CAL_REQ);
    }
    Cursor cur = null;
    ContentResolver cr = getContentResolver();

    String[] mProjection =
            {
                    CalendarContract.Attendees.ATTENDEE_NAME,
                    CalendarContract.Attendees.ATTENDEE_RELATIONSHIP,
                    CalendarContract.Attendees.ATTENDEE_EMAIL,
                    CalendarContract.Attendees.ATTENDEE_TYPE,
            };

    Uri uri = CalendarContract.Attendees.CONTENT_URI;
    String selection = CalendarContract.Attendees.ATTENDEE_TYPE + " = ? ";
    String[] selectionArgs = new String[]{""+CalendarContract.Attendees.TYPE_REQUIRED};

    cur = cr.query(uri, mProjection, selection, selectionArgs, null);

    while (cur.moveToNext()) {
        String name = cur.getString(cur.getColumnIndex(CalendarContract.Attendees.ATTENDEE_NAME));

        TextView tv1 =  new TextView(this);
        tv1.setText(name);
        cont.addView(tv1);
    }

}
 

Adding Data to Attendees Table Using Calendar Provider API

 public void addAttendee(View view) {

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CALENDAR}, MY_CAL_WRITE_REQ);
    }

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

    ContentValues values = new ContentValues();
    values.put(CalendarContract.Attendees.ATTENDEE_NAME, "KTR");
    values.put(CalendarContract.Attendees.ATTENDEE_TYPE, CalendarContract.Attendees.TYPE_REQUIRED);
    values.put(CalendarContract.Attendees.ATTENDEE_EMAIL, "ktr@example.com");
    values.put(CalendarContract.Attendees.ATTENDEE_STATUS, CalendarContract.Attendees.ATTENDEE_STATUS_INVITED);
    values.put(CalendarContract.Attendees.ATTENDEE_RELATIONSHIP, CalendarContract.Attendees.RELATIONSHIP_SPEAKER);
    values.put(CalendarContract.Attendees.EVENT_ID, "1");

    cr.insert(CalendarContract.Attendees.CONTENT_URI, values);
}
 

Modifying Attendees Data Using Calendar Provider API

 public void updateAttendee(View view) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CALENDAR}, MY_CAL_WRITE_REQ);
    }
    ContentValues contentValues = new ContentValues();
    contentValues.put(CalendarContract.Attendees.ATTENDEE_TYPE, CalendarContract.Attendees.TYPE_OPTIONAL);

    Uri uri = CalendarContract.Attendees.CONTENT_URI;

    String mSelectionClause = CalendarContract.Attendees.ATTENDEE_STATUS+ " = ? AND ";
    mSelectionClause = mSelectionClause+ CalendarContract.Attendees.EVENT_ID + " = ?";
    String[] mSelectionArgs = {""+CalendarContract.Attendees.ATTENDEE_STATUS_INVITED, "1"};

    int updCount = getContentResolver().update(uri, contentValues,mSelectionClause,mSelectionArgs);

}
 

Deleting Attendees Data Using Calendar Provider API

 public void deleteAttendee(View view) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CALENDAR}, MY_CAL_WRITE_REQ);
    }

    Uri uri = CalendarContract.Attendees.CONTENT_URI;

    String mSelectionClause = CalendarContract.Attendees.ATTENDEE_STATUS+ " = ?";
    String[] mSelectionArgs = {""+CalendarContract.Attendees.ATTENDEE_STATUS_DECLINED};

    int updCount = getContentResolver().delete(uri,mSelectionClause,mSelectionArgs);

}
 

Getting Data from Reminders Table Using Calendar Provider API

 public void addReminder(View view) {

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CALENDAR}, MY_CAL_WRITE_REQ);
    }

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

    ContentValues values = new ContentValues();
    values.put(CalendarContract.Reminders.EVENT_ID, "2");
    values.put(CalendarContract.Reminders.MINUTES, "25");
    values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALARM);

    cr.insert(CalendarContract.Reminders.CONTENT_URI, values);
}
 

Adding Data to Reminders Table Using Calendar Provider API

 public void getDataFromRemindersTable(View v) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CALENDAR}, MY_CAL_REQ);
    }
    Cursor cur = null;
    ContentResolver cr = getContentResolver();

    String[] mProjection =
            {
                    CalendarContract.Reminders.METHOD,
                    CalendarContract.Reminders.MINUTES,
            };

    Uri uri = CalendarContract.Reminders.CONTENT_URI;
    String selection = CalendarContract.Reminders.EVENT_ID + " = ? ";
    String[] selectionArgs = new String[]{"2"};

    cur = cr.query(uri, mProjection, selection, selectionArgs, null);

    while (cur.moveToNext()) {
        String minut = cur.getString(cur.getColumnIndex(CalendarContract.Reminders.MINUTES));

        TextView tv1 =  new TextView(this);
        tv1.setText(minut);
        cont.addView(tv1);
    }

}
 

Modifying Reminders Data Using Calendar Provider API

 public void updateReminder(View view) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CALENDAR}, MY_CAL_WRITE_REQ);
    }
    ContentValues contentValues = new ContentValues();
    contentValues.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_EMAIL);

    Uri uri = CalendarContract.Reminders.CONTENT_URI;

    String mSelectionClause = CalendarContract.Reminders.EVENT_ID+ " = ? AND ";
    mSelectionClause = mSelectionClause+ CalendarContract.Reminders.METHOD +" = ?";
    String[] mSelectionArgs = {"2", ""+CalendarContract.Reminders.METHOD_ALARM};

    int updCount = getContentResolver().update(uri, contentValues,mSelectionClause,mSelectionArgs);

}
 

Deleting Reminders Data Using Calendar Provider API

 public void deleteReminder(View view) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CALENDAR}, MY_CAL_WRITE_REQ);
    }

    Uri uri = CalendarContract.Reminders.CONTENT_URI;

    String mSelectionClause = CalendarContract.Reminders.EVENT_ID+ " = ?";
    String[] mSelectionArgs = {"2"};

    int updCount = getContentResolver().delete(uri,mSelectionClause,mSelectionArgs);

}