How to add events to default calendar in android

In android we can add events to default calendar by invoking calendar application from our application. All we want to do is to create an intent with necessary parameters required for creating event and then open the calendar app using intent. In this tutorial we will learn how to add events to event calendar in android.

Steps

  1. Create new project in android studio
  2. Add the button into your layout for opening default calendar
  3. Implement the code for adding events to default calendar within the onclick of the button

Create New project in android studio

Refer Android beginners app development guide if you are beginner or if you don’t know how to create project in android studio

Add the button for opening default calendar

Now we need to have a button for stimulating the action. The app will open default calendar for adding events on click of this button. Place the following code in your layout file for displaying the button in the activity.

<Button
    android:id="@+id/btn_add_event"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="Add event"
    android:background="@color/colorPrimary"
    android:textColor="#FFFFFF"/>

Implement the code for adding events to default calendar within the onclick of the button

After adding button to the layout file you need to have the reference of the button in the activity’s java file. The default calendar should popup for adding the event while clicking on button. For achieving the same you just need to add the following code into the java file.

Button btn_add_event = (Button) findViewById(R.id.btn_add_event);
btn_add_event.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        Calendar calendarEvent = Calendar.getInstance();
        Intent intent = new Intent(Intent.ACTION_EDIT);
        intent.setType("vnd.android.cursor.item/event");
        intent.putExtra("beginTime", calendarEvent.getTimeInMillis());
        intent.putExtra("endTime", calendarEvent.getTimeInMillis() + 60 * 60 * 1000);
        intent.putExtra("title", "Sample Event");
        intent.putExtra("allDay", true);
        intent.putExtra("rule", "FREQ=YEARLY");
        startActivity(intent);

    }
});

output

event-calendar

About the author

Hi guys, i am the author of codesfor. I am a B.Tech graduate currently working as an App developer. Apart from job i am a blogger and a freelancer.

2 Comments

  1. Aleena Augustine July 4, 2019 Reply
  2. Sufyan January 21, 2019 Reply

Add a Comment

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