How to set Calendar Reminder in Android Kotlin.

Harshita Bambure
2 min readDec 5, 2022

--

Today we will learn about setting reminder in our google calendar in Android Kotlin.

In build.Gradle (: app) file we need to app build feature.

android {
buildFeatures {
viewBinding true
}
}

Now we will create our XML design in the activity_main.xml file.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tl_title"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:hint="My reminder title"
android:textColorHint="?android:attr/textColorPrimary"
app:boxStrokeColor="?attr/colorControlNormal"
app:boxStrokeWidthFocused="2dp"
app:endIconMode="clear_text"
app:endIconTint="?attr/colorControlNormal"
app:hintTextColor="?android:attr/textColorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:startIconTint="?attr/colorControlNormal">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/et_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:textColor="?android:attr/textColorPrimary" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tl_loc"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:hint="Enter meeting location"
android:textColorHint="?android:attr/textColorPrimary"
app:boxStrokeColor="?attr/colorControlNormal"
app:boxStrokeWidthFocused="2dp"
app:endIconMode="clear_text"
app:endIconTint="?attr/colorControlNormal"
app:hintTextColor="?android:attr/textColorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tl_title"
app:startIconTint="?attr/colorControlNormal">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/et_loc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:textColor="?android:attr/textColorPrimary" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tl_desc"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:hint="Description"
android:textColorHint="?android:attr/textColorPrimary"
app:boxStrokeColor="?attr/colorControlNormal"
app:boxStrokeWidthFocused="2dp"
app:endIconMode="clear_text"
app:endIconTint="?attr/colorControlNormal"
app:hintTextColor="?android:attr/textColorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tl_loc"
app:startIconTint="?attr/colorControlNormal">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/et_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLines="6"
android:textColor="?android:attr/textColorPrimary" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.button.MaterialButton
android:id="@+id/btn_add_event"
android:text="Add event to calender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
app:layout_constraintTop_toBottomOf="@id/tl_desc"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>


</androidx.constraintlayout.widget.ConstraintLayout>

Here we code for the MainActivity.kt file.

package com.example.calendarreminder

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.CalendarContract
import android.widget.Toast
import androidx.appcompat.app.AppCompatDelegate
import com.example.calendarreminder.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

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

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
binding.btnAddEvent.setOnClickListener(){
if (!binding.etTitle.getText().toString().isEmpty() && !binding.etLoc.getText().toString().isEmpty() && !binding.etDesc
.getText().toString().isEmpty()) {
val intent = Intent(Intent.ACTION_INSERT)
intent.setData(CalendarContract.Events.CONTENT_URI)
intent.putExtra(CalendarContract.Events.TITLE,binding.etTitle.getText().toString())
intent.putExtra(CalendarContract.Events.DESCRIPTION,binding.etDesc.getText().toString())
intent.putExtra(CalendarContract.Events.EVENT_LOCATION,binding.etLoc.getText().toString())
intent.putExtra(CalendarContract.Events.ALL_DAY,"true")
intent.putExtra(Intent.EXTRA_EMAIL,"")
if(intent.resolveActivity(getPackageManager()) != null){
startActivity(intent)
finish()
}else{
Toast.makeText(this, "There is no app that support this action", Toast.LENGTH_SHORT).show()
}

}else{
Toast.makeText(this, "Please fill all the fields",
Toast.LENGTH_SHORT).show();
}
}
}
override fun onBackPressed() {
super.onBackPressed()
}
}

Last and important is we need to add some uses permission and in the intent filter, we added action and data in Android Manifest file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.calendarreminder">

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

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.CalendarReminder"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.INSERT" />
<data android:mimeType="vnd.android.cursor.dir/event" />
<category android:name="android.intent.category.DEFAULT" />

</intent-filter>
</activity>
</application>

</manifest>

--

--

Harshita Bambure

Android Developer || WomenTech Global Ambassador at WomenTech Network. || Yoga Teacher || Member @WTM .