Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Android small notification icon color depending on context

I've been scouring stack overflow for days to find the answer to this question, but I cannot figure it out for the life of me :-/

On my Samsung S6 with Android O, apps like Gmail have white notifications for the status bar but red/blue/green/whatever notifications when you pull the notifications down to inspect them individually. See for example the attached photos - Gmail red when the notifications are displayed and white in the notification bar.

What type of programatic Android sorcery must one do to accomplish this? More specifically, in Notification.Builder, what values do I need to set to get it to display this way? Are there two different icons? Is there one drawable with multiple layers? If so, how does one choose which layers are shown where? Thanks!

beautiful red color for gmail stark white color for gmail

like image 650
mikesol Avatar asked Nov 20 '17 04:11

mikesol


People also ask

How do I change the color of my notification shade Android?

From the main settings menu, "Notification Theme" allows you to change the background color of your notifications. You can enable a dark theme, light theme, the system default, or choose from a selection of colors.

Can Android change notification icons?

Currently users can't change the notification icon, but I could make it possible to select one of the 300+ Automate icons. A custom picture isn't possible since Android requires the images to be packaged within the app.


1 Answers

First of all, we should be using NotificationCompat.Builder

Secondly, here is the code to get that behavior

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, "NOTIFICATION_CHANNEL_NAME")
            .setSmallIcon(R.drawable.notification_icon)
            .setContentTitle(title)
            .setContentText(message)
            .setColor(ContextCompat.getColor(context, R.color.your_color))
            .setContentIntent(pendingIntent);

There is one more thing, the icon that you are using (R.drawable.notification_icon) should be white and should have a little bit of opacity (it should be just a little transparent). After that, your top notification icon will be white and when you draw notifications, it will become R.color.your_color

like image 137
Bugs Happen Avatar answered Oct 14 '22 02:10

Bugs Happen