Retrieve the default calendar id in Android
Solution 1
To get the list of calendars, you need to query the ContentResolver like this:
public MyCalendar [] getCalendar(Context c) {
String projection[] = {"_id", "calendar_displayName"};
Uri calendars;
calendars = Uri.parse("content://com.android.calendar/calendars");
ContentResolver contentResolver = c.getContentResolver();
Cursor managedCursor = contentResolver.query(calendars, projection, null, null, null);
if (managedCursor.moveToFirst()){
m_calendars = new MyCalendar[managedCursor.getCount()];
String calName;
String calID;
int cont= 0;
int nameCol = managedCursor.getColumnIndex(projection[1]);
int idCol = managedCursor.getColumnIndex(projection[0]);
do {
calName = managedCursor.getString(nameCol);
calID = managedCursor.getString(idCol);
m_calendars[cont] = new MyCalendar(calName, calID);
cont++;
} while(managedCursor.moveToNext());
managedCursor.close();
}
return m_calendars;
}
Solution 2
I used following code to get the calendar ID.
private fun getCalendarId(context: Context) : Long? {
val projection = arrayOf(Calendars._ID, Calendars.CALENDAR_DISPLAY_NAME)
var calCursor = context.contentResolver.query(
Calendars.CONTENT_URI,
projection,
Calendars.VISIBLE + " = 1 AND " + Calendars.IS_PRIMARY + "=1",
null,
Calendars._ID + " ASC"
)
if (calCursor != null && calCursor.count <= 0) {
calCursor = context.contentResolver.query(
Calendars.CONTENT_URI,
projection,
Calendars.VISIBLE + " = 1",
null,
Calendars._ID + " ASC"
)
}
if (calCursor != null) {
if (calCursor.moveToFirst()) {
val calName: String
val calID: String
val nameCol = calCursor.getColumnIndex(projection[1])
val idCol = calCursor.getColumnIndex(projection[0])
calName = calCursor.getString(nameCol)
calID = calCursor.getString(idCol)
Log.d("Calendar name = $calName Calendar ID = $calID")
calCursor.close()
return calID.toLong()
}
}
return null
}
So do not pass 0, 1 or 3 as Calendar ID. Use above function instead.
Also, check if Calendar ID is null
and do not perform operations with it if it is null
.
val calendarId = getCalendarId(context)
if (calendarId != null) {
//Call operations e.g.: Insert operation
}
Solution 3
There's a IS_PRIMARY
column in CalendarContract.CalendarColumns
. You query with selection:
CalendarContract.CalendarColumns.IS_PRIMARY + "=1"
However, this is since SDK 17
Solution 4
Some latest versions have issue, there is different visible calendar list so below is the code to select PRIMARY calendar and in older devices this query return 0 record, so used second one if 1st one return 0 records.
Cursor calCursor = mContext.getContentResolver().query(CalendarContract.Calendars.CONTENT_URI, projection, CalendarContract.Calendars.VISIBLE + " = 1 AND " + CalendarContract.Calendars.IS_PRIMARY + "=1", null, CalendarContract.Calendars._ID + " ASC");
if(calCursor.getCount() <= 0){
calCursor = mContext.getContentResolver().query(CalendarContract.Calendars.CONTENT_URI, projection, CalendarContract.Calendars.VISIBLE + " = 1", null, CalendarContract.Calendars._ID + " ASC");
}
Related videos on Youtube
Gold
Updated on August 22, 2022Comments
-
Gold almost 2 years
I have the following code for adding event to calendar.
The problem is that I don't know how to retrieve the default calendar id.
long calID = 3; long startMillis = 0; long endMillis = 0; Calendar beginTime = Calendar.getInstance(); beginTime.set(2013, 3, 23, 7, 30); startMillis = beginTime.getTimeInMillis(); Calendar endTime = Calendar.getInstance(); endTime.set(2013, 3, 24, 8, 45); endMillis = endTime.getTimeInMillis(); ContentResolver cr = getContentResolver(); ContentValues values = new ContentValues(); values.put(Events.DTSTART, startMillis); values.put(Events.DTEND, endMillis); values.put(Events.TITLE, "My Test"); values.put(Events.DESCRIPTION, "My Calendar Test"); values.put(Events.CALENDAR_ID, calID); values.put(Events.EVENT_TIMEZONE, "Israel/tel-aviv"); Uri uri = cr.insert(Events.CONTENT_URI, values);
The line:
long calID = 3;
is the calendar idIs it possible to get the default calendar id from Android, or do I need to show a list of calendars and have the user pick one?
If not possible, how to show the list of calendar accounts?
-
yoad w almost 6 yearsSorry for late post, this might be useful for future followers. It seems the IS_PRIMARY attribute is not unique. I have several accounts on my calendar list (university, private, work) and they all seem to be marked as primary. The only ones who are not primary are the birthdays and holidays. (I ran the above code to verify).
-
RonTLV over 4 yearsrequires requires android.permission.READ_CALENDAR
-
K Pradeep Kumar Reddy about 4 yearsYes i also have 2 accounts signed into my device. so i'm getting 2 entries marked as primary.
-
K Pradeep Kumar Reddy about 4 yearsIf there are multiple primary entries, then probably we can use the calendar ID of any of those entries to create events right ??
-
Nicolas almost 4 years@KPradeepKumarReddy There might be multiple primary entries if multiple accounts are registered on the device. So that depends on which account you want to use.