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
- Create new project in android studio
- Add the button into your layout for opening default calendar
- 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
I want to make an attendence register using calender view in android. In which we can mark attendence on calender and the admin must be able to see the absent students on firebase database
How to download source code