0

I would like to ask if it is possible to make programmatically the colour of the notification icon "responsive" to the status bar colour. That means if the status bar is white (or some other light colours) then the icon will be displayed dark and if it is black (or some other dark colour) then the icon will be displayed white. I also have created two variants of the icon - white one (icon_white) and black one (icon_black) and I also could create a notification with the white icon so far but when the status bar is white, the icon can't be seen. My code is here below:

White icon one light and black backgrounds

Intent intent = new 
Intent(MainActivity.this, MainActivity.class); 
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
android.support.v4.app.NotificationCompat.Builder builder = new android.support.v4.app.NotificationCompat.Builder(MainActivity.this, "id 1")
.setSmallIcon(R.drawable.icon_white)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentTitle("Notification title")
.setContentText("Notification text")
.setPriority(android.support.v4.app.NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setAutoCancel(true);

android.support.v4.app.NotificationManagerCompat notificationManager = android.support.v4.app.NotificationManagerCompat.from(MainActivity.this);
notificationManager.notify(1, builder.build());

Thank you very much for your help bilykralik16

  • For latest Android versions, you need the notification icon to be white in color with transparent background. Also you can set the icon color if you target >= lollipop using `setColor` – Md. Asaduzzaman Oct 29 '19 at 07:38

2 Answers2

0

You can save a shared preference color value for the theme. Use the setColor value with the saved them color.

.setColor(ContextCompat.getColor(getApplicationContext(), color.color_default))

 return new Notification.Builder(getApplicationContext(), ANDROID_CHANNEL_ID)
                .setContentTitle(title)
                .setContentText(body)
                .setSmallIcon(getNotificationIcon())
                .setContentIntent(pendingIntent)
                .setColor(ContextCompat.getColor(getApplicationContext(), R.color.color_default))
                .setVisibility(Notification.VISIBILITY_PUBLIC) //to show content on lock screen
                .setPriority(Notification.PRIORITY_MAX)
                .setAutoCancel(true);
Jane Alam
  • 360
  • 2
  • 7
0

Thank you all very much but I think I was misunderstood . I thought about the icon colour in the status bar but when the status bar isn't "open". The situation is visible on the picture I posted. When the status bar has a dark colour, the white icon is visible. But when the status bar has a light colour, of course the white icon can't be seen and I just wanted to ask if it is possible to change that icon colour based on status bar colour (white status bar = black icon, black status bar = white icon). I wanted to be the same colour as other icons (see picture) like Wi-Fi, time etc.