19

When showing a notification in android N and O it shows like this enter image description here

You can change the top color of the icon and the app name by using setColor method of the NotificationCompat.Builder, The problem is that the displayed color doesn't match the provided one as it gets darker in the notification.

I tested it on Google Pixel(Oreo). Is this documented somewhere? Any workarounds?

Ahmed Hegazy
  • 12,395
  • 5
  • 41
  • 64

1 Answers1

29

Google decided to restrict acceptable colors in Notification when using setColor. Yes, it's undocumented, unsolicited and evil! After some digging (hacker's way) it may be found in the source code of (internal) ContrastColorUtil. Inspect line 300 (findContrastColor) and 475 (resolveContrastColor) where the rationale is:

Resolves a Notification's color such that it has enough contrast to be used as the color for the Notification's action and header text.

I do not agree. Google should have known better and be doing no evil to the freedom of aesthetic choices of designers and developers.

Nuno Henriques
  • 731
  • 1
  • 9
  • 13
  • 1
    Is there any workaround for this now, or any document where it is stated that it will add enough contrast to given color – Rohit Raich May 03 '19 at 10:27
  • 1
    Still have the same problem. Any solution ? – cesarsicas Oct 22 '20 at 21:14
  • The source code link is dead. Do you have any further info about this? I want to know what color can I use to satisfy the minimum contrast. – Henrique Bruno May 01 '22 at 07:50
  • Found another answer with more info - https://stackoverflow.com/a/40917609 – Henrique Bruno May 01 '22 at 08:02
  • Hey, thanks for the comment. I've update the post and fixed Web links to Google's Android source code, added direct links to referenced source code lines. The source code was refactored and the name changed from NotificationColorUtil to ContrastColorUtil... – Nuno Henriques May 02 '22 at 12:14