articles

Home / DeveloperSection / Articles / Calendar API in Android

Calendar API in Android

Manoj Pandey8796 19-Mar-2015

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 on calendars, events, attendees, reminders, and so on.

The Calendar Provider API can be used by 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. 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 for example

Calendar API in Android

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

  

After choose your date

 

Calendar API in Android


Updated 03-Feb-2020

Leave Comment

Comments

Liked By