Calendar Application

1

 

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"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    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" />  
  
</LinearLayout>  

3.)   Put INTERNET permission to manifest file:


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


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[] { "Lars.Vogel@gmail.com",  
                "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.

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

Proudly designed by Theme Junkie.