0
val calID: Long? = getCalendarId(context)
                            val startMillis: Long = Calendar.getInstance().run {
                                set(2024, 3, 27, 16, 45)
                                timeInMillis
                            }
                            val endMillis: Long = Calendar.getInstance().run {
                                set(2024, 3, 27, 16, 50)
                                timeInMillis
                            }
 
                            val values = ContentValues().apply {
                                put(CalendarContract.Events.DTSTART, startMillis)
                                put(CalendarContract.Events.DTEND, endMillis)
                                put(CalendarContract.Events.TITLE, "gym")
                                put(CalendarContract.Events.DESCRIPTION, "Group workout")
                                put(CalendarContract.Events.CALENDAR_ID, calID)
                                put(CalendarContract.Events.EVENT_TIMEZONE, "Asia/Kolkata")
                            }
                            Log.d("Calendar", "Event values: $values")
 
                            val uri: Uri? = context.contentResolver.insert(CalendarContract.Events.CONTENT_URI, values)
                            Log.d("Calendar", "Event URI: $uri")
 
                            val eventID: Long = uri?.lastPathSegment?.toLong() ?: 0

In the above code when I add the date. it is adding the event after one month from that date. Why does it behave like that?

private fun getCalendarId(context: Context) : Long? {
    val projection = arrayOf(CalendarContract.Calendars._ID, CalendarContract.Calendars.CALENDAR_DISPLAY_NAME)
 
    var calCursor = context.contentResolver.query(
        CalendarContract.Calendars.CONTENT_URI,
        projection,
        CalendarContract.Calendars.VISIBLE + " = 1 AND " + CalendarContract.Calendars.IS_PRIMARY + "=1",
        null,
        CalendarContract.Calendars._ID + " ASC"
    )
 
    if (calCursor != null && calCursor.count <= 0) {
        calCursor = context.contentResolver.query(
            CalendarContract.Calendars.CONTENT_URI,
            projection,
            CalendarContract.Calendars.VISIBLE + " = 1",
            null,
            CalendarContract.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)
 
         
 
            calCursor.close()
            return calID.toLong()
        }
    }
    return null
}

Can I find the default calendar selected by the user or Google calender with this code? can I specify the calendar to which I want to add the event?

I want to add the event to a specific Calender on the phone. How can i achieve that?

1 Answer 1

0

The issue with the date discrepancy may be related to how you are setting the month in your Calendar instances. In Java's Calendar class, months are zero-based, meaning January is represented as 0, February as 1, and so on. So, when you set the month to 3, it actually represents April, not March as you might expect.

To fix this, you should subtract 1 from the month when setting the date:

val startMillis: Long = Calendar.getInstance().run {
    set(2024, 2, 27, 16, 45) // March (2), 27th
    timeInMillis
}

val endMillis: Long = Calendar.getInstance().run {
    set(2024, 2, 27, 16, 50) // March (2), 27th
    timeInMillis
}

Regarding specifying the calendar to which you want to add the event, your getCalendarId() function attempts to find the primary calendar (IS_PRIMARY) and returns its ID. This should work fine for many devices where the primary calendar is typically the default calendar. However, it's not guaranteed to work on all devices as the behavior may vary depending on the calendar apps installed and their configurations.

If you want to add the event to a specific calendar, you can modify the getCalendarId() function to return the ID of that specific calendar. You can achieve this by querying the calendars based on certain criteria such as calendar name or account name. Here's an example:

private fun getCalendarId(context: Context, calendarName: String): Long? {
    val projection = arrayOf(CalendarContract.Calendars._ID, CalendarContract.Calendars.CALENDAR_DISPLAY_NAME)

    val selection = "${CalendarContract.Calendars.CALENDAR_DISPLAY_NAME} = ?"
    val selectionArgs = arrayOf(calendarName)

    context.contentResolver.query(
        CalendarContract.Calendars.CONTENT_URI,
        projection,
        selection,
        selectionArgs,
        null
    )?.use { cursor ->
        if (cursor.moveToFirst()) {
            return cursor.getLong(cursor.getColumnIndex(CalendarContract.Calendars._ID))
        }
    }

    return null
}

Then, when you want to add an event, you can pass the desired calendar name to this function to get the ID, and use that ID when inserting the event into the calendar:

val calendarName = "My Calendar"
val calID: Long? = getCalendarId(context, calendarName)

This will ensure that the event is added to the specified calendar. Make sure to handle cases where the specified calendar is not found or the function returns null.

Not the answer you're looking for? Browse other questions tagged or ask your own question.