Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To add an event to the Google Calendar API in Android Studio, you first need to set up the Google Calendar API client and authenticate the user. Then, you can use the following code to create a new event:

Event event = new Event()
    .setSummary("Event Summary")
    .setLocation("Event Location")
    .setDescription("Event Description");

DateTime startDateTime = new DateTime("2019-10-10T09:00:00-07:00");
EventDateTime start = new EventDateTime().setDateTime(startDateTime);
event.setStart(start);

DateTime endDateTime = new DateTime("2019-10-10T17:00:00-07:00");
EventDateTime end = new EventDateTime().setDateTime(endDateTime);
event.setEnd(end);

String[] recurrence = new String[] {"RRULE:FREQ=DAILY;COUNT=2"};
event.setRecurrence(Arrays.asList(recurrence));

EventAttendee[] attendees = new EventAttendee[] {
    new EventAttendee().setEmail("attendee.email@example.com"),
};
event.setAttendees(Arrays.asList(attendees));

EventReminder[] reminders = new EventReminder[] {
    new EventReminder().setMethod("email").setMinutes(24 * 60),
    new EventReminder().setMethod("popup").setMinutes(10),
};
event.setReminders(Arrays.asList(reminders));

String calendarId = "primary";
event = mService.events().insert(calendarId, event).execute();
System.out.printf("Event created: %s\n", event.getHtmlLink());

In the above code:

  • Event is the object that represents an event in the Google Calendar API.
  • setSummary(), setLocation(), and setDescription() are methods that set the basic information of the event, such as the summary, location, and description.
  • EventDateTime represents a date and time in the event, and is used to set the start and end times of the event.
  • setRecurrence() sets the recurrence of the event, such as how often it repeats.
  • EventAttendee represents an attendee of the event, and is used to add attendees to the event.
  • EventReminder represents a reminder for the event, and is used to set reminders for the event.
  • events().insert() is the method that actually inserts the event into the calendar.

You should replace the placeholders with actual values for the event information, and replace mService and calendarId with the Google Calendar API client and the calendar ID, respectively.