Android - Create Custom Calendar with Event

metinkale38 picture metinkale38 · Dec 12, 2014 · Viewed 11.6k times · Source

I have an App which shows special Days. I want to integrate them into the calendar.

The events are static, they don't change, so I don't have to update the calendar very often.

I first thought of creating a local calendar and add the events, but new android versions (since 2.3?) seem not to support that; to implement I would have to create a Calendar Provider.

I saw this project on github: https://github.com/dschuermann/birthday-adapter. It is very complicated; its main use is adding the birthdays of the contacts to a new calendar.

There is lots of code, much of which I don't think I need. Do I really need to register to android's account manager to integrate a Calendar Provider? I just need a new Calendar with my event...

Would it be easier to take the user's default Calendar and add all the Events there? I could add some identifiers to the description, to be able to remove the events if the user doesn't want them.

Any tips, tutorials, or further readings are appreciated.

Metin Kale

Answer

Andriy Dychka picture Andriy Dychka · Dec 12, 2014

You can create events in your device calendar via Intent. I think it could be useful for you.

public long addEventToCalender(ContentResolver cr, String title, String addInfo, String place, int status,
                                      long startDate, boolean isRemind,long endDate) {
    String eventUriStr = "content://com.android.calendar/events";
    ContentValues event = new ContentValues();
    // id, We need to choose from our mobile for primary its 1
    event.put("calendar_id", 1);
    event.put("title", title);
    event.put("description", addInfo);
    event.put("eventLocation", place);
    event.put("eventTimezone", "UTC/GMT +2:00");

    // For next 1hr
    event.put("dtstart", startDate);
    event.put("dtend", endDate);
    //If it is bithday alarm or such kind (which should remind me for whole day) 0 for false, 1 for true
    // values.put("allDay", 1);
    //  event.put("eventStatus", status);
    event.put("hasAlarm", 1);

    Uri eventUri = cr.insert(Uri.parse(eventUriStr), event);
    long eventID = Long.parseLong(eventUri.getLastPathSegment());

    if (isRemind) {
        String reminderUriString = "content://com.android.calendar/reminders";
        ContentValues reminderValues = new ContentValues();
        reminderValues.put("event_id", eventID);
        // Default value of the system. Minutes is a integer
        reminderValues.put("minutes", 5);
        // Alert Methods: Default(0), Alert(1), Email(2), SMS(3)
        reminderValues.put("method", 1);
        cr.insert(Uri.parse(reminderUriString), reminderValues); //Uri reminderUri =
    }
    return eventID;
}

For more information visit http://developer.android.com/reference/java/util/Calendar.html