How to insert calendar events in your Android app

There are several ways to insert calendar events in an Android app. Using Intents, ContentResolvers and SyncAdapter. Let us understand how to insert events with the help of intents through an example.

In this example, we will create events for the classes to be conducted every week.

Course name, start date and time are entered through the app screen. With the click of a button, the calendar app should be opened and the user should be able to save the event after verifying the details.

Add calendar read and write permissions in the android manifest file.

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

Design the layout file as shown below.

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_batch_header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="60dp"
        android:text="@string/create_batches"
        android:textSize="30sp"
        android:textColor="@color/black"
        android:textStyle="bold"/>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/et_coursename_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="8dp"
        android:hint="@string/course_name"
        android:textColorHint="@color/black"
        app:boxStrokeColor="@color/black"
        app:boxCornerRadiusTopStart="50dp"
        app:boxCornerRadiusBottomEnd="50dp"
        app:boxCornerRadiusBottomStart="50dp"
        app:boxCornerRadiusTopEnd="50dp"
        app:boxBackgroundColor="@color/white"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/et_coursename_input"
            android:textColor="@color/black"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </com.google.android.material.textfield.TextInputLayout>


    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/et_day_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="8dp"
        android:hint="@string/day_of_the_week"
        app:boxCornerRadiusTopStart="50dp"
        app:boxCornerRadiusBottomEnd="50dp"
        app:boxCornerRadiusBottomStart="50dp"
        app:boxCornerRadiusTopEnd="50dp"
        app:boxBackgroundColor="@color/white"
        android:textColorHint="@color/black"
        app:boxStrokeColor="@color/black"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/et_day_input"
            android:textColor="@color/black"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/et_starttime_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="8dp"
        android:hint="@string/start_time"
        android:textColorHint="@color/black"
        app:boxStrokeColor="@color/black"
        app:boxCornerRadiusTopStart="50dp"
        app:boxCornerRadiusBottomEnd="50dp"
        app:boxCornerRadiusBottomStart="50dp"
        app:boxCornerRadiusTopEnd="50dp"
        app:boxBackgroundColor="@color/white"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/et_starttime_input"
            android:textColor="@color/black"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/et_endtime_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="8dp"
        android:hint="@string/end_time"
        android:textColorHint="@color/black"
        app:boxStrokeColor="@color/black"
        app:boxCornerRadiusTopStart="50dp"
        app:boxCornerRadiusBottomEnd="50dp"
        app:boxCornerRadiusBottomStart="50dp"
        app:boxCornerRadiusTopEnd="50dp"
        app:boxBackgroundColor="@color/white"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/et_endtime_input"
            android:textColor="@color/black"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </com.google.android.material.textfield.TextInputLayout>

    <Button
        android:id="@+id/btn_create_event"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="@string/create_batch"
        android:layout_marginTop="24dp"
        android:layout_gravity="center"/>

</androidx.appcompat.widget.LinearLayoutCompat>

To insert calendar events we need to provide start and end time values in timeInMillis format. Since we are entering the values as string fields in the app, first convert them to timeInMillis. To do that, extract the date, month and year details from the start_date field, and hour and minutes details from the start_time and end_time fields.

Set the recurrence rule frequency to weekly and then invoke the INSERT INTENT as shown in the code below.

package com.spcreations.calendereventexample

import android.content.Intent
import android.os.Bundle
import android.provider.CalendarContract
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.spcreations.calendereventexample.databinding.ActivityMainBinding
import java.text.SimpleDateFormat
import java.util.*

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)


        val class_name = binding.etCoursenameInput
        val start_date = binding.etDayInput
        val start_time = binding.etStarttimeInput
        val end_time = binding.etEndtimeInput
        val createBtn = binding.btnCreateEvent

        createBtn.setOnClickListener {
            if (!class_name.text.toString().isEmpty() && !start_date.text.toString()
                    .isEmpty() && !start_time.text.toString().isEmpty()
                && !end_time.text.toString().isEmpty()
            ) {

                // Extract date,month,year and time from the date and time strings provided
                val startTime = start_date.text.toString() + " " + start_time.text.toString()
                val endTime = start_date.text.toString() + " " + end_time.text.toString()
                Log.i("TAG", "Start time " + startTime + " End time " + endTime)
                val dateFormat = SimpleDateFormat("dd-MM-yyyy HH:mm", Locale.getDefault())
//Create calendar instance and set the date and time of the event
                val calendar = Calendar.getInstance()
                calendar.time = dateFormat.parse(startTime)

// Extract the year,month, date, hour and minute values
                val l_year = calendar.get(Calendar.YEAR)
                val l_month = calendar.get(Calendar.MONTH)
                val l_date = calendar.get(Calendar.DATE)
                val l_start_hour = calendar.get(Calendar.HOUR_OF_DAY)
                val l_start_minute = calendar.get(Calendar.MINUTE)

                Log.i(
                    "TAG",
                    "Date " + l_date + " Month " + l_month + " Year " + l_year + " Start time " + l_start_hour + " Start minute " + l_start_minute
                )
//Convert the date string and time string values entered in Millis format.
                val startMillis: Long =
                    Calendar.getInstance().run {
                        set(l_year, l_month, l_date, l_start_hour, l_start_minute)
                        timeInMillis
                    }


                calendar.time = dateFormat.parse(endTime.toString())

                val l_end_hour = calendar.get(Calendar.HOUR_OF_DAY)
                val l_end_minute = calendar.get(Calendar.MINUTE)

                val endMillis: Long = Calendar.getInstance().run {
                    set(l_year, l_month, l_date, l_end_hour, l_end_minute)
                    timeInMillis
                }

                val recurrenceRule = "FREQ=WEEKLY"

                val intent = Intent(Intent.ACTION_INSERT)
                    .setData(CalendarContract.Events.CONTENT_URI)
                    .putExtra(CalendarContract.Events.TITLE, class_name.text.toString())
                    .putExtra(CalendarContract.Events.EVENT_LOCATION, "Online")
                    .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startMillis)
                    .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endMillis)
                    .putExtra(CalendarContract.Events.RRULE, recurrenceRule)
                    .putExtra(
                        CalendarContract.Events.AVAILABILITY,
                        CalendarContract.Events.AVAILABILITY_BUSY
                    )
                startActivity(intent)

            } else {
                Toast.makeText(this, "Please fill all  the fields", Toast.LENGTH_SHORT).show()
            }

        }
    }
}

Enter the values as shown below.

On click of the create event, the calendar app is opened and event details are inserted. Verify the details and save the event.

This event is created with weekly recurrence and can be observed in the calendar.