Calendar Application

 

This example explains how to call and use system calendar into your application.

Algorithm:

 

1.)   Create a new project by File-> New -> Android Project name it CalendarApplication.

2.)   Write following into main.xml file:

 

<!--?xml version="1.0" encoding="utf-8"?-->
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >  

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="Create Event" />  

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="queryCalendar"
        android:text="Query Calendar" />  


3.)   Put INTERNET permission to manifest file:


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

4.)   Run for output.

Steps:

 

1.) Create a project named CalendarApplication and set the information as stated in the image.

Build Target: Android 4.0

Application Name: CalendarApplication

Package Name: com. example. CalendarApplication

Activity Name: CalendarApplicationActivity

Min SDK Version: 14

2.) Open CalendarApplicationActivity.java file and write following code there:


package com.example.CalendarApplication;

import java.util.GregorianCalendar;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Calendars;
import android.provider.CalendarContract.Events;
import android.view.View;
import android.widget.Toast;

public class CalendarApplicationActivity  extends Activity {  

    public static final String[] EVENT_PROJECTION = new String[] {
            Calendars._ID, // 0
            Calendars.ACCOUNT_NAME, // 1
            Calendars.CALENDAR_DISPLAY_NAME // 2
    };  

    private static final int PROJECTION_DISPLAY_NAME_INDEX = 2;  

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }  

    public void onClick(View view) {  

        Intent intent = new Intent(Intent.ACTION_INSERT);
        intent.setType("vnd.android.cursor.item/event");
        intent.putExtra(Events.TITLE, "Learn Android");
        intent.putExtra(Events.EVENT_LOCATION, "Home suit home");
        intent.putExtra(Events.DESCRIPTION, "Download Examples");  

        GregorianCalendar calDate = new GregorianCalendar(2012, 10, 02);
        intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,
                calDate.getTimeInMillis());
        intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,
                calDate.getTimeInMillis());  

        intent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true);  

        intent.putExtra(Events.RRULE,
                "FREQ=WEEKLY;COUNT=11;WKST=SU;BYDAY=TU,TH");  

        intent.putExtra(Events.ACCESS_LEVEL, Events.ACCESS_PRIVATE);
        intent.putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY);  

        startActivity(intent);  

    }  

    public void queryCalendar(View view) {
        Cursor cur = null;
        ContentResolver cr = getContentResolver();
        Uri uri = Calendars.CONTENT_URI;
        String selection = "((" + Calendars.ACCOUNT_NAME + " = ?) AND ("
                + Calendars.ACCOUNT_TYPE + " = ?))";  

        String[] selectionArgs = new String[] { "[email protected]",
                "com.google" };
        cur = cr.query(uri, EVENT_PROJECTION, selection, selectionArgs, null);  

        while (cur.moveToNext()) {
            String displayName = null;
            displayName = cur.getString(PROJECTION_DISPLAY_NAME_INDEX);
            Toast.makeText(this, "Calendar " + displayName, Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

3.) Compile and build the project.

 

Output

 

 

 

5.)   If your phone doesn’t have any default calendar installed you will see the screen as below. Follow the instructions and see your calendar.

About the Author

Leave a Reply 5 comments

Lokeswararao Gandreddi - August 6, 2012 Reply

Why It was not supporting in Emulator, It show setup couldn’t finish… I tried to create account, But it shows above error message.

Billy - October 18, 2012 Reply

Thank you for sharing superb informations. Your website is very cool. I’m impressed by the details that you’ve on this website. It reveals how nicely you understand this subject. Bookmarked this web page, will come back for more articles. You, my friend, ROCK! I found simply the information I already searched all over the place and just could not come across. What a great site.

ganesh - April 11, 2013 Reply

its working good with google calendars. thanks. need to make it working in hour level.

vijaya - September 12, 2013 Reply

How can i add calendar please tel me

Pierre - November 4, 2013 Reply

Hey Chetana,

Thanks for posting this demo. I have an error on this code: “setContentView(R.layout.main); ” It’s compalining about main not defined. Where do I define it.

Thanks

Leave a Reply: