80

I was going through the Notifications design pattern, and didn't find anything that talks about notification icon background. As you probably noticed, there is only a light grey background for custom notifications. But apps like Hangouts, or simply the USB Debugging notification has a custom color for their notification icon background.

Is there any possibility to change that grey into something else? (that specific circle's color programmatically)

See picture

Zsolt Boldizsar
  • 2,447
  • 2
  • 27
  • 37
  • may be they have special icon with green background? – Ahmed Hegazy Dec 07 '14 at 14:42
  • Not sure if it is still important to anyone but since Android 6.1 there is a new flag "NotificationCompat.Builder.setColorized(boolean colorize)" which allows changing the "gray background" of a notification. – Tobias Reich Dec 01 '17 at 13:58

3 Answers3

161

1) Obtain Color

int color = 0xff123456;
int color = getResources().getColor(R.color.my_notif_color);
int color = ContextCompat.getColor(context, R.color.my_notif_color);

2) Set the Color to the Notification

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
...
builder.setColor(color);
Notification notif = builder.build();

The color is respected only on Lollipop and only affects background of the small icon. If a large icon is shown its contents are entirely your responsibility.

Source: NotificationCompat.Builder#setColor(int)

Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
  • 11
    According to this: http://stackoverflow.com/a/27023679/327011 the setColor will only change the color of the small icon. Beware. – neteinstein Dec 12 '14 at 10:03
  • 5
    1) This only works on `LOLLIPOP`. 2) This does not affect the image provided by you in `setLargeBitmap`. It only colors background of small icon. – Eugen Pechanec Dec 17 '14 at 00:04
  • @AlexVPerl you can try my solution just below this comment – satyapol Oct 01 '15 at 06:33
  • 1
    "setColor" is the correct answer IF YOU DO NOT SET A LARGE ICON. You will have the small icon display big with my_notif_color as background, no small badge added. tested on android 6.0 – Loda Oct 21 '15 at 07:52
  • I have a problem i set the icon have green colour but when notification appear it changes to white automatically. – HUSNAIN SARWAR Jul 21 '16 at 07:06
  • @HUSNAINSARWAR Icon *background* is set by `setColor`. Icon *foreground* is always white. All of it is white, that's why you use a silhouette for a notification. Not your launcher icon. – Eugen Pechanec Oct 25 '16 at 12:08
  • can you share correct sample code with icons and sizes for me please am trying this but no use white square icon only coming – Harsha Nov 16 '16 at 13:25
  • @Harsha https://blog.clevertap.com/fixing-notification-icon-for-android-lollipop-and-above/ – Eugen Pechanec Nov 16 '16 at 13:29
  • Any problems with Samsung devices? In my case `setColor` works on most devices except Samsung S7 with 7.0... – Ziem May 23 '17 at 09:37
  • @Ziem Do you have a screenshot? – Eugen Pechanec May 23 '17 at 14:52
  • @EugenPechanec http://imgur.com/lEmLRsY as you can see text is colored but icon is not. It works OK on all emulators and on my 5X but not on Samsung S7 (7.0). But I managed to solve my issue. It appears Samsung can't handle 144px icons with #fffffe color. I rescaled and recolored it and it started working. – Ziem May 25 '17 at 06:47
  • @Ziem 144px is overkill. Notification icon should be 4x24dp on a xxxhdpi device. I believe it's the color. AOSP always rewrites all colors in the icon. Looks like Samsung retains color information if the drawable is not pure white. Good catch! – Eugen Pechanec May 25 '17 at 06:58
  • @EugenPechanec it's inherited code and you are right 144px it's an overkill. Thankfully it works now ;). Thank you for your interest! – Ziem May 25 '17 at 09:35
  • is it possible to change grayed out background color without customizing xml template for notification ? – Ahmad Apr 16 '20 at 05:07
10

if you've defined color in colors.xml then in your NotificationBuilder add value as

.setColor(getResources().getColor(R.color.<YOUR_COLOR>))

That should solve your problem. It only affect to background of the icon.

satyapol
  • 983
  • 11
  • 16
3

getColor(int) has been deprecated on Resources

We should now use one of these alternatives:

Luís Ramalho
  • 10,018
  • 4
  • 52
  • 67