Wednesday, 27 March 2013

How to add events to native calendar App in Android

Today, Let's see how we can add events to user native calendar App in Android. The Calendar App is a most common application that users rely upon on their device. Applications can access the Calendar App using the new APIs available in Android 4.0. Adding new events to the Calendar is easy and  requires special Read and Write calendar  application permissions.

Let's demonstrate this by an example. This example will contain Add Events Button, but clicking on that button add Event Calendar screen will get displayed.

Create a new android application project from eclipse with these below settings

Application Name: EventCalendar
Project Name: EventCalendar
Package Name: com.example.eventcalendar
Target SDK: Android 4.2 (Choose a SDK that suits your test devices and favored emulator configuraton)

In Package Explorer, create activity_eventcalendar.xml file under res->layout folder and paste the below xml code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add Events" />
</LinearLayout>

In Package Explorer, Create EventCalendarActivity.java file under
src->com.example.eventcalendar  folder and paste the below code
package com.example.eventcalendar;

import java.util.Calendar;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class EventCalendarActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_eventcalendar);

    Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View arg0) {

         // Perform action on click
         addCalendarEvent();
      }

    });

  }

  public void addCalendarEvent(){

    Calendar cal = Calendar.getInstance();     
    Intent intent = new Intent(Intent.ACTION_EDIT);
    intent.setType("vnd.android.cursor.item/event");
    intent.putExtra("beginTime", cal.getTimeInMillis());
    intent.putExtra("allDay", true);
    intent.putExtra("rrule", "FREQ=YEARLY");
    intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
    intent.putExtra("title", "A Test Event from android app");
    intent.putExtra("description", "A Test Description from android app");
    intent.putExtra("eventLocation", "Geolocation");
    startActivity(intent);

  }
}

Explanation:
Calendar cal = Calendar.getInstance();

Above method returns a calendar whose locale is based on system settings and whose time fields have been initialized with the current date and time.
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");

Above Intent class will launch the calendar app screen that will display add event form for add/edit purpose.
intent.putExtra("beginTime", cal.getTimeInMillis());
intent.putExtra("allDay", true);
intent.putExtra("rrule", "FREQ=YEARLY");
intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
intent.putExtra("title", "A Test Event from android app");
intent.putExtra("description", "A Test Description from android app");
intent.putExtra("eventLocation", "Geolocation");

Intent Extras are used to supply Event Information to Calendar, which contain title of the Event, its description, start and end date and so on.These details will be set in a  form that displays for the user to confirm the event in their calendar. There are several other intent extras you can set as well. For detailed information, please visit here.

startActivity() is used to launch a new activity  or get an existing activity to do something new.

Final Thing:

In AndroidManifest.xml, Place the below activity XML code inside <application>

<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<activity
            android:name="com.example.eventcalendar.EventCalendarActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
</activity>


Thats it, Now you can run your application, by right clicking Package Name, then select Run As->Android Application.

Important Note: The user is responsible for configuring the Calendar application with the appropriate Calendar accounts (e.g. Google Account, Microsoft Exchange).

Hope, you enjoyed this Post.

6 comments:

  1. what if i have to add events using API Level 9 or above ?

    ReplyDelete
  2. Hi :)
    Thank you for this tutorial.
    I'm a total beginner and I get some errors I can't solve. Maybe someone can help me with them.

    1.
    In this line of my java-file: Calendar cal = Calendar.getInstance();
    it says: "The method getInstance() is undefined for the type Calendar

    2.
    Here: intent.putExtra("beginTime", cal.getTimeInMillis());
    it says: The method getTimeInMillis() is undefined for the type Calendar

    3.
    And finally here: intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
    The method getTimeInMillis() is undefined for the type Calendar

    Thanks again :)

    ReplyDelete
    Replies
    1. Hi,

      Thanks for contacting me. Please check whether you have imported the Android Java library classes

      import java.util.Calendar;
      import android.os.Bundle;
      import android.app.Activity;
      import android.content.Intent;

      Thanks,
      Suresh dot Ariya

      Delete

Related Posts Plugin for WordPress, Blogger...