Calendar Application

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:

 

[code lang=”xml”]
<!–?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" />

[/code]

3.)   Put INTERNET permission to manifest file:

[code lang=”java”]

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

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

[/code]

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:

[code lang=”java”]

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();
}
}
}

[/code]

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.

Comments 6

  1. Lokeswararao Gandreddi

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

  2. 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.

  3. 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

  4. Hi,
    How do i add the calendar on the emulator? The emulator already has a calendar, but not sure how to add it? Can we use that Calendar instead?

    Thanks,
    Neet

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">