Search code examples
androidandroid-calendar

Changing the color of an Android Calendar Event


In my App, i create calendar events for specific dates. As i want them to be in the color of my app (i want them to be fast recognizable) my question is, how to realize that. Here is what i have:

Intent intent = new Intent(Intent.ACTION_INSERT);
                intent.setType("vnd.android.cursor.item/event");
                intent.putExtra(CalendarContract.Events.TITLE, "Some Title");
                intent.putExtra(CalendarContract.Events.DESCRIPTION, "Description");

                String dateTime = date.getTime().replace(" Uhr", "");
                dateTime += date.getDate().substring(date.getDate().lastIndexOf(" "));
                SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm dd.MM.yyyy", Locale.GERMAN);
                java.util.Date d = null;
                try {
                    d = dateFormat.parse(dateTime);
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                assert d != null;
                intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, d.getTime());

here i want to set the color

                intent.putExtra(CalendarContract.Events.DISPLAY_COLOR, Color.YELLOW);

                myViewHolder.dateView.getContext().startActivity(intent);

Here is a picture of the thing i want to change in the calendar App


Solution

  • EVENT_COLOR is documented to be written by the sync adapter of the account the calendar belongs to. Only the app 'owning' the calendar should write these values:

    This should only be updated by the sync adapter for a given account.

    It might be possible for your App to force specific values when directly accessing the calendar (that is, not through an intent but by directly querying the ContentResolver, if you have the appropriate permissions). However, they could get overwritten by the 'owning' app without notice, do not necessarily synchronize across devices and could cause other issues down the road. I do not recommend that.

    The documentation is not as clear for EVENT_COLOR_KEY, so that column might be an option if there is an existing event color suitable for your application (note that existing event colors may depend on both the calendar and the user, do not hardcode color keys!). I'd assume that direct use of ContentResolver will be necessary, using an Intent only works if the calendar app selected by the user supports that feature (I don't know if / how many calendar apps support that).


    If you need custom colors that work independently of the existing calendar colors and calendar app installed, you could create your own calendar which is tied to your app. Assigning a color to a whole calendar is straightforward (CALENDAR_COLOR). As your app is the owner of that calendar, you can then freely alter any aspect of every event.

    However, these events are not part of your user's existing calendar: they are not synced across devices and do not provide any vendor-specific features (if any). Unless your events are static for all users, I'd not recommend to go down that route.