Color a Google Calendar event

2

I am creating events in the Google calendar and I want to assign a specific color to the event, but I do not get it, I get the color of the calendar.

The code I use is the following, which creates the event perfectly.

 public static long addUVEvent (Activity activity, long calendarId, String title, String description, String location, Date dateBegin, Date dateEnd ,boolean allDay, boolean needReminder, boolean needMailService){

        long startMillisEpoch;
        long endMillisEpoch;

        // calculamos la fecha de inicio y la de fin
        Calendar beginTime = Calendar.getInstance();
        beginTime.setTime(dateBegin);
        startMillisEpoch = beginTime.getTimeInMillis();

        Calendar endTime = Calendar.getInstance();
        endTime.setTime(dateEnd);
        endMillisEpoch = endTime.getTimeInMillis();

        //Preparamos el evento
        ContentValues eventValues = new ContentValues();

        eventValues.put(CalendarContract.Events.CALENDAR_ID, calendarId);
        eventValues.put(CalendarContract.Events.TITLE, title);
        eventValues.put(CalendarContract.Events.EVENT_LOCATION, location);
        eventValues.put(CalendarContract.Events.DESCRIPTION, description);

        eventValues.put(CalendarContract.Events.DTSTART, startMillisEpoch);
        eventValues.put(CalendarContract.Events.DTEND, endMillisEpoch);

//      A secondary color key for the individual event. NULL or an empty string are reserved for indicating that the event does not use a key for looking up the color.
//      The provider will update EVENT_COLOR automatically when a valid key is written to this column.
//      The key must reference an existing row of the CalendarContract.Colors table. @see Colors
//        eventValues.put(CalendarContract.Events.EVENT_COLOR_KEY, eventColor); //string
        //eventValues.put(CalendarContract.Events.EVENT_COLOR_KEY, Color.parseColor("blue"));
        eventValues.put(CalendarContract.Events.EVENT_COLOR, Color.BLUE);

        // NOTA: Todos los eventos deben tener un timezone.
        // Sino la aplicacion lanzara un IllegalArgumentException.
        eventValues.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getDisplayName());
        eventValues.put(CalendarContract.Events.EVENT_END_TIMEZONE, TimeZone.getDefault().getDisplayName());

        eventValues.put(CalendarContract.Events.ALL_DAY, (allDay)?1:0); 

        eventValues.put(CalendarContract.Events.GUESTS_CAN_INVITE_OTHERS, 1);
        eventValues.put(CalendarContract.Events.GUESTS_CAN_MODIFY, 0);
        eventValues.put(CalendarContract.Events.GUESTS_CAN_SEE_GUESTS, 1);

//        eventValues.put(CalendarContract.Events.ACCESS_LEVEL, CalendarContract.Events.ACCESS_DEFAULT);
//        // Default(0),          CalendarContract.Events.ACCESS_DEFAULT
//        // Confidential(1),     CalendarContract.Events.ACCESS_CONFIDENTIAL
//        // Private(2),          CalendarContract.Events.ACCESS_PRIVATE
//        // Public(3),           CalendarContract.Events.ACCESS_PUBLIC

//        eventValues.put(CalendarContract.Events.STATUS , CalendarContract.Events.STATUS_TENTATIVE);
//        // This information is sufficient for most entries
//        // tentative (0),       CalendarContract.Events.STATUS_TENTATIVE
//        // confirmed (1)        CalendarContract.Events.STATUS_CONFIRMED
//        // or canceled (2)      CalendarContract.Events.STATUS_CANCELED

//        eventValues.put(CalendarContract.Events.AVAILABILITY, CalendarContract.Events.AVAILABILITY_FREE);
//        // Busy(0),             CalendarContract.Events.AVAILABILITY_BUSY
//        // Free(1),             CalendarContract.Events.AVAILABILITY_FREE
//        // Tentative(2),        CalendarContract.Events.AVAILABILITY_TENTATIVE

//        eventValues.put(CalendarContract.Events.HAS_ALARM, 1); // 0 for false, 1 for true

//        eventValues.put(CalendarContract.Events.RRULE, "FREQ=YEARLY;UNTIL=20080222T000000Z"); // The recurrence rule for the event FREQ=WEEKLY;COUNT=10;WKST=SU;BYDAY=TU,TH
//        eventValues.put(CalendarContract.Events.RRULE, CalendarIntentHelper.EVENT_TYPE_YEARLY);
//        eventValues.put(CalendarContract.Events.EXRULE, 1); //The recurrence exception rule for the event
//
//        eventValues.put(CalendarContract.Events.RDATE, 1); // The recurrence dates for the event
//        eventValues.put(CalendarContract.Events.EXDATE, 1); //The recurrence exception dates for the event

        //for one hour
//        eventValues.put(CalendarContract.Events.DURATION, "+P1H");//The duration of the event in RFC2445 format, only for recurring events

        // insert event to calendar

        if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {

            PermissionUtil.requestCalendarPermission(activity);

            return -1;
        }

        Uri calUri = null;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            calUri = activity.getContentResolver().insert(CalendarContract.Events.CONTENT_URI, eventValues);
        }else{
            calUri = activity.getContentResolver().insert(Uri.parse(eventUriString), eventValues);
        }

        if (calUri != null) {

            long eventID = -1;
            try {
                eventID = Long.parseLong(calUri.getLastPathSegment());
            } catch (Exception e) {
                return -1;
            }

            if(eventID != -1) {
                if (needReminder) {
                    /***************** Event: Reminder(with alert) Adding reminder to event *******************/

                    ContentValues reminderValues = new ContentValues();

                    reminderValues.put(CalendarContract.Reminders.EVENT_ID , eventID);
                    reminderValues.put(CalendarContract.Reminders.MINUTES , 5); // Default value of the system. Minutes is a integer
//                    reminderValues.put(CalendarContract.Reminders.MINUTES,  60 * 12); //12 horas
                    reminderValues.put(CalendarContract.Reminders.METHOD , CalendarContract.Reminders.METHOD_ALERT);
                    // Alert Methods:
                    // Default(0),      CalendarContract.Reminders.METHOD_DEFAULT
                    // Alert(1),        CalendarContract.Reminders.METHOD_ALERT
                    // Email(2),        CalendarContract.Reminders.METHOD_EMAIL
                    // SMS(3),          CalendarContract.Reminders.METHOD_SMS
                    // ALARM(4),        CalendarContract.Reminders.METHOD_ALARM


                    Uri reminderUri = null;

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                        reminderUri = activity.getContentResolver().insert(CalendarContract.Reminders.CONTENT_URI, reminderValues);
                    }else{
                        reminderUri = activity.getContentResolver().insert(Uri.parse(reminderUriString), reminderValues);
                    }
                }

                /***************** Event: Meeting(without alert) Adding Attendies to the meeting *******************/

                if (needMailService) {

                    /********
                     * To add multiple attendees need to insert ContentValues multiple
                     * times
                     ***********/
                    ContentValues attendeesValues = new ContentValues();

                    attendeesValues.put(CalendarContract.Attendees.EVENT_ID , eventID);
                    attendeesValues.put(CalendarContract.Attendees.ATTENDEE_NAME , "pepe"); // Attendees name
                    attendeesValues.put(CalendarContract.Attendees.ATTENDEE_EMAIL , "[email protected]");// Attendee  Email id
                    attendeesValues.put(CalendarContract.Attendees.ATTENDEE_RELATIONSHIP , CalendarContract.Attendees.RELATIONSHIP_NONE);
                    // Relationship_None(0),    CalendarContract.Attendees.RELATIONSHIP_NONE
                    // Relationship_Attendee(1),CalendarContract.Attendees.RELATIONSHIP_ATTENDEE
                    // Organizer(2),            CalendarContract.Attendees.RELATIONSHIP_ORGANIZER
                    // Performer(3),            CalendarContract.Attendees.RELATIONSHIP_PERFORMER
                    // Speaker(4)               CalendarContract.Attendees.RELATIONSHIP_SPEAKER
                    attendeesValues.put(CalendarContract.Attendees.ATTENDEE_TYPE , CalendarContract.Attendees.TYPE_NONE);
                    // None(0),                 CalendarContract.Attendees.TYPE_NONE
                    // Required(1),             CalendarContract.Attendees.TYPE_REQUIRED
                    // Optional(2),             CalendarContract.Attendees.TYPE_OPTIONAL
                    attendeesValues.put(CalendarContract.Attendees.ATTENDEE_STATUS , CalendarContract.Attendees.ATTENDEE_STATUS_NONE);
                    // NOne(0),                 CalendarContract.Attendees.ATTENDEE_STATUS_NONE
                    // Accepted(1),             CalendarContract.Attendees.ATTENDEE_STATUS_ACCEPTED
                    // Decline(2),              CalendarContract.Attendees.ATTENDEE_STATUS_DECLINED
                    // Invited(3),              CalendarContract.Attendees.ATTENDEE_STATUS_INVITED
                    // Tentative(4)             CalendarContract.Attendees.ATTENDEE_STATUS_TENTATIVE

                    Uri attendeuesesUri = null;

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                        attendeuesesUri = activity.getContentResolver().insert(CalendarContract.Attendees.CONTENT_URI, attendeesValues);
                    }else{
                        attendeuesesUri = activity.getContentResolver().insert(Uri.parse(attendeesUriString), attendeesValues);
                    }
                }
            }

            return eventID;
        }else{
            return -1;
        }
    }
    
asked by Joacer 19.09.2016 в 16:36
source

2 answers

2

According to your code, you are using a blue color:

 eventValues.put(CalendarContract.Events.EVENT_COLOR, Color.BLUE);

With

  

EVENT_COLOR You can change the color of an individual event.

and you can use the RGB of the color you want, simply add the hexadecimal color, for example a color blue light:

eventValues.put(CalendarContract.Events.EVENT_COLOR, Color.parseColor("#ADD8E6));

You can also use the constant defined in the SDK, as in the case of your code that is the blue color:

eventValues.put(CalendarContract.Calendars.EVENT_COLOR, Color.BLUE);
    
answered by 20.09.2016 / 02:00
source
1

Try this line:

eventValues.put(CalendarContract.Events.EVENT_COLOR, Color.parseColor("#000000"));

instead of this:

eventValues.put(CalendarContract.Events.EVENT_COLOR, Color.BLUE);
    
answered by 19.09.2016 в 19:56