Changing notification icon background on Lollipop

61,210

Solution 1

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)

Solution 2

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.

Solution 3

getColor(int) has been deprecated on Resources

We should now use one of these alternatives:

Share:
61,210

Related videos on Youtube

Zsolt Boldizsar
Author by

Zsolt Boldizsar

Updated on July 05, 2022

Comments

  • Zsolt Boldizsar
    Zsolt Boldizsar almost 2 years

    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

    • Ahmed Hegazy
      Ahmed Hegazy over 9 years
      may be they have special icon with green background?
    • Tobias Reich
      Tobias Reich over 6 years
      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.
  • neteinstein
    neteinstein over 9 years
    According to this: stackoverflow.com/a/27023679/327011 the setColor will only change the color of the small icon. Beware.
  • Eugen Pechanec
    Eugen Pechanec over 9 years
    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.
  • satyapol
    satyapol over 8 years
    @AlexVPerl you can try my solution just below this comment
  • Ahmed Jihad
    Ahmed Jihad over 8 years
    "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
  • HUSNAIN SARWAR
    HUSNAIN SARWAR almost 8 years
    I have a problem i set the icon have green colour but when notification appear it changes to white automatically..
  • HUSNAIN SARWAR
    HUSNAIN SARWAR almost 8 years
    I have a problem i set the icon have green colour but when notification appear it changes to white automatically.
  • HUSNAIN SARWAR
    HUSNAIN SARWAR almost 8 years
    I have a problem i set the icon have green colour but when notification appear it changes to white automatically.
  • Eugen Pechanec
    Eugen Pechanec over 7 years
    @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
    Eugen Pechanec over 7 years
    If you're using a solid color the old method works just fine.
  • Harsha
    Harsha over 7 years
    still white square icon onlycoming in 6.0 but before 5.0 appicon fine
  • Harsha
    Harsha over 7 years
    can you share correct sample code with icons and sizes for me please am trying this but no use white square icon only coming
  • Eugen Pechanec
    Eugen Pechanec over 7 years
  • Ziem
    Ziem about 7 years
    Any problems with Samsung devices? In my case setColor works on most devices except Samsung S7 with 7.0...
  • Eugen Pechanec
    Eugen Pechanec about 7 years
    @Ziem Do you have a screenshot?
  • Ziem
    Ziem about 7 years
    @EugenPechanec 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.
  • Eugen Pechanec
    Eugen Pechanec about 7 years
    @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!
  • Ziem
    Ziem about 7 years
    @EugenPechanec it's inherited code and you are right 144px it's an overkill. Thankfully it works now ;). Thank you for your interest!
  • Ahmad
    Ahmad about 4 years
    is it possible to change grayed out background color without customizing xml template for notification ?