---
title: "Calendar API in Android"  
description: "In this article I am explaining about Calendar API in Android"  
author: "Manoj Pandey"  
published: 2015-03-19  
updated: 2020-02-03  
canonical: https://www.mindstick.com/articles/1674/calendar-api-in-android  
category: "android"  
tags: ["android"]  
reading_time: 3 minutes  

---

# Calendar API in Android

The Calendar Provider is a repository for a user's calendar events. The Calendar Provider API allows you to perform query, insert, update, and delete [operations](https://www.mindstick.com/news/2675/bmw-agrees-to-integrate-blockchain-with-operations-partners-with-bnb-chain-coinweb) on calendars, events, attendees, reminders, and so on.\

The Calendar Provider API can be used by [applications](https://www.mindstick.com/articles/12847/how-to-choose-the-right-ethernet-cable-for-industrial-applications) and sync adapters. The rules vary depending on what type of program is making the calls. This document focuses primarily on using the Calendar Provider API as an [application](https://www.mindstick.com/articles/12824/calculator-application-in-android). For a discussion of how sync adapters are different, see Sync Adapters.

If we read or write data from calendar then we have need to add permission in [AndroidManifest.xml file](https://www.mindstick.com/interview/2588/explain-androidmanifest-xml-file-in-detail) for example

![Calendar API in Android](https://www.mindstick.com/mindstickarticle/6d68184e-7929-44ad-a24d-84ba18d10984/images/47c42f4c-9f07-4cf9-a67b-b11b0e22a9f9.png)

##### Here I am creating example of Calendar which set events, for a particular date.

##### \
1. Create an android project and api level must be greater than 13

##### \
2. Add permission in AndroidManifest.xml file

```
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.androidcalendar"    android:versionCode="1"    android:versionName="1.0" >     <uses-sdk        android:minSdkVersion="16"        android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.READ_CALENDAR" /> <uses-permission android:name="android.permission.WRITE_CALENDAR" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.androidcalendar.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />           <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application> </manifest>
```

**3**. Add calendar view on your activity_main.xml file

```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@android:color/darker_gray"    android:orientation="vertical"    android:padding="20dp" >     <CalendarView        android:id="@+id/calendar"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_margin="10dp" /> </LinearLayout> 
```

##### 4. My color resources

```
<?xml version="1.0" encoding="utf-8"?><resources>    <item name="transparent" type="color">#00FFFFFF</item>    <item name="grey" type="color">#555555</item>    <item name="green" type="color">#4499CC00</item>    <item name="darkgreen" type="color">#FF669900</item> </resources>
```

##### 5. Add following code in MainActivity.class

```
package com.example.androidcalendar; import java.util.GregorianCalendar; import android.os.Bundle;import android.provider.CalendarContract;import android.provider.CalendarContract.Events;import android.widget.CalendarView;import android.widget.CalendarView.OnDateChangeListener;import android.app.Activity;import android.content.Intent; public class MainActivity extends Activity {     CalendarView calendar;      @Override     protected void onCreate(Bundle savedInstanceState) {           super.onCreate(savedInstanceState);            // sets the main layout of the activity           setContentView(R.layout.activity_main);            // initializes the calendarview           initializeCalendar();     }      public void initializeCalendar() {           calendar = (CalendarView) findViewById(R.id.calendar);            // sets whether to show the week number.           calendar.setShowWeekNumber(false);            // sets the first day of week according to Calendar.           // here we set Monday as the first day of the Calendar           calendar.setFirstDayOfWeek(2);            // The background color for the selected week.      calendar.setSelectedWeekBackgroundColor(getResources().getColor(                     R.color.green));            // sets the color for the dates of an unfocused month.          calendar.setUnfocusedMonthDateColor(getResources().getColor(                     R.color.transparent));            // sets the color for the separator line between weeks.           calendar.setWeekSeparatorLineColor(getResources().getColor(                     R.color.transparent));     // sets the color for the vertical bar shown at the beginning and      //  at the end of the selected date.           calendar.setSelectedDateVerticalBar(R.color.darkgreen);         // sets the listener to be notified upon selected date change.         calendar.setOnDateChangeListener(new OnDateChangeListener() {                 // show the selected date as a toast                @Override          public void onSelectedDayChange(CalendarView view, int year,                           int month, int day) {                  Intent calIntent = new Intent(Intent.ACTION_INSERT);                   calIntent.setType("vnd.android.cursor.item/event");                calIntent.putExtra(Events.TITLE, "My BirthDay Party");                 calIntent.putExtra(Events.EVENT_LOCATION, "My City");           calIntent.putExtra(Events.DESCRIPTION, "My 22th birthday");   GregorianCalendar calDate = new GregorianCalendar(year, month,day);       calIntent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true);     calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,                                calDate.getTimeInMillis());          calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,                                calDate.getTimeInMillis());                      startActivity(calIntent);                                }           });      }}
```

Now run your application

Choose your event date

![Calendar API in Android](https://www.mindstick.com/mindstickarticle/6d68184e-7929-44ad-a24d-84ba18d10984/images/5348bbe0-f2dc-4564-87f3-b4b8d295be80.png)

After [choose your](https://www.mindstick.com/articles/23258/choose-your-business-structure-wisely) date

![Calendar API in Android](https://www.mindstick.com/mindstickarticle/6d68184e-7929-44ad-a24d-84ba18d10984/images/be896fd6-17f7-430c-b440-92da47bb754e.png)

---

Original Source: https://www.mindstick.com/articles/1674/calendar-api-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
