How to Set Calendar Event in Android?
In most Android devices, a Calendar is an application that displays a calendar and is most commonly used to set reminders or events. Calendar applications in general are very light-weighted and consume less memory. Calendar has a background thread running that keeps a track of when to alert the user about a particular event or a reminder. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Kotlin language.
So in this article, we will show you how you could programmatically set a calendar event in Android. Follow the below steps once the IDE is ready.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Add a Button as shown below.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout 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" > < Button android:id = "@+id/button_1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "click" android:layout_centerInParent = "true" /> </ RelativeLayout > |
Step 3: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package org.w3wiki.setcalendarevent import android.annotation.SuppressLint import android.content.Intent import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Button import java.text.SimpleDateFormat import java.util.* class MainActivity : AppCompatActivity() { @SuppressLint ( "SimpleDateFormat" ) override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Declaring and initializing // the button from the layout file val mButton = findViewById<Button>(R.id.button_1) // Event start and end time with date val startTime = "2022-02-1T09:00:00" val endTime = "2022-02-1T12:00:00" // Parsing the date and time val mSimpleDateFormat = SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss" ) val mStartTime = mSimpleDateFormat.parse(startTime) val mEndTime = mSimpleDateFormat.parse(endTime) // When Button is clicked, Intent started // to create an event with given time mButton.setOnClickListener { val mIntent = Intent(Intent.ACTION_EDIT) mIntent.type = "vnd.android.cursor.item/event" mIntent.putExtra( "beginTime" , mStartTime.time) mIntent.putExtra( "time" , true ) mIntent.putExtra( "rule" , "FREQ=YEARLY" ) mIntent.putExtra( "endTime" , mEndTime.time) mIntent.putExtra( "title" , "w3wiki Event" ) startActivity(mIntent) } } } |
Output:
You can see that when the button is clicked, the event is created in the Calendar app.