Calendar event write in android

you can follow these steps:

  1. Add the following permission in the AndroidManifest.xml file
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
  1. Create a new Calendar instance:
val calendar = Calendar.getInstance()
  1. Set the time and date for the event:
calendar.set(Calendar.YEAR, year) calendar.set(Calendar.MONTH, month) calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth) calendar.set(Calendar.HOUR_OF_DAY, hourOfDay) calendar.set(Calendar.MINUTE, minute)

Replace year, month, dayOfMonth, hourOfDay, and minute with the values you want for your event.

  1. Create a new ContentValues instance to store the event information:
val values = ContentValues().apply { put(CalendarContract.Events.TITLE, title) put(CalendarContract.Events.DESCRIPTION, description) put(CalendarContract.Events.EVENT_LOCATION, location) put(CalendarContract.Events.DTSTART, calendar.timeInMillis) put(CalendarContract.Events.DTEND, calendar.timeInMillis) put(CalendarContract.Events.CALENDAR_ID, calendarId) put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().id) }

Replace title, description, and location with the values you want for your event. calendar.timeInMillis sets the start and end time for the event. calendarId is the ID of the calendar you want to add the event to. You can obtain the ID using the CalendarContract.Calendars._ID field.

  1. Insert the event using a ContentResolver instance:
val cr = context.contentResolver val uri = cr.insert(CalendarContract.Events.CONTENT_URI, values) val eventId = uri?.lastPathSegment?.toLong()

Replace context with your activity or application context. eventId is the ID of the newly created event.

That's it! Your event should now be added to the calendar.


Second Method


you can follow these steps:

  1. Add the following permission in the AndroidManifest.xml file:
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
  1. Create a new instance of the CalendarProviderManager class to get access to the calendar provider:
val cr = contentResolver val calendarUri = CalendarContract.Calendars.CONTENT_URI val calendars = cr.query(calendarUri, null, null, null, null) calendars?.moveToFirst() val calendarId = calendars?.getString(calendars.getColumnIndex(CalendarContract.Calendars._ID))
  1. Create a new instance of the ContentValues class and add the details of the event:
val values = ContentValues().apply { put(CalendarContract.Events.DTSTART, startTimeMillis) put(CalendarContract.Events.DTEND, endTimeMillis) put(CalendarContract.Events.TITLE, "Event Title") put(CalendarContract.Events.DESCRIPTION, "Event Description") put(CalendarContract.Events.CALENDAR_ID, calendarId) put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().id) }
  1. Insert the event into the calendar using the CalendarProviderManager class:
val eventUri = cr.insert(CalendarContract.Events.CONTENT_URI, values) val eventId = eventUri?.lastPathSegment?.toLong()
  1. (Optional) You can also set reminders for the event using the following code:
val reminderValues = ContentValues().apply { put(CalendarContract.Reminders.MINUTES, 10) put(CalendarContract.Reminders.EVENT_ID, eventId) put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT) } cr.insert(CalendarContract.Reminders.CONTENT_URI, reminderValues)

Note: You may need to handle permissions, exceptions and null checks accordingly.

Comments

Popular posts from this blog

How to Integrate or Work with Open Street Map (OSM) in an Android App (Kotlin)

ListView in android with api and volley Network Library in android Studio.

how to implement OpenStreetMap in android