Changing notification icon background on Lollipop
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:
-
Resources
's getColor(int, Theme) -
ContextCompat
's getColor(Context context, int id)
Related videos on Youtube
Zsolt Boldizsar
Updated on July 05, 2022Comments
-
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)
-
Ahmed Hegazy over 9 yearsmay be they have special icon with green background?
-
Tobias Reich over 6 yearsNot 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 over 9 yearsAccording to this: stackoverflow.com/a/27023679/327011 the setColor will only change the color of the small icon. Beware.
-
Eugen Pechanec over 9 years1) This only works on
LOLLIPOP
. 2) This does not affect the image provided by you insetLargeBitmap
. It only colors background of small icon. -
satyapol over 8 years@AlexVPerl you can try my solution just below this comment
-
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 almost 8 yearsI have a problem i set the icon have green colour but when notification appear it changes to white automatically..
-
HUSNAIN SARWAR almost 8 yearsI have a problem i set the icon have green colour but when notification appear it changes to white automatically.
-
HUSNAIN SARWAR almost 8 yearsI have a problem i set the icon have green colour but when notification appear it changes to white automatically.
-
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 over 7 yearsIf you're using a solid color the old method works just fine.
-
Harsha over 7 yearsstill white square icon onlycoming in 6.0 but before 5.0 appicon fine
-
Harsha over 7 yearscan 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 over 7 years@Harsha blog.clevertap.com/…
-
Ziem about 7 yearsAny problems with Samsung devices? In my case
setColor
works on most devices except Samsung S7 with 7.0... -
Eugen Pechanec about 7 years@Ziem Do you have a screenshot?
-
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 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 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 about 4 yearsis it possible to change grayed out background color without customizing xml template for notification ?