How to use different notification icon in Android for notification area and notification drawer

I am setting the icon using a .png in a white transparent background. It works fine when displayed on the lock screen and in the notification area.

Icon highlighted in yellow: enter image description here

Now, in the notification drawer, the icon also appears in white (it's the same icon). enter image description here

But I would like to use a different icon, in this case the truck in blue. Right now I am setting the color to blue using Notification.Builder.SetColor () and the title is displayed in that color.

You can see how the Gmail icon is displayed in white in the status bar and red in the inbox.

This is the actual code for generating the notification. Code for Xamarin.Droid

var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;

Notification.Builder builder = new Notification.Builder(this)
                .SetContentTitle(title)
                .SetContentText(desc)
                .SetSmallIcon(Routes.Droid.Resource.Drawable.truck)
                .SetAutoCancel(true);
builder.SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.truckColor));
builder.SetColor(Android.Graphics.Color.Rgb(33,150,243));

var notification = new Notification.BigTextStyle(builder).BigText(desc).Build();

notificationManager.Notify(-1, notification);

      

How do I set the color of the icon in the notification drawer or use different icons?

The goal is to display the icon in white in the notification area and blue in the notification drawer.

+3


source to share


1 answer


Icon color is set automatically on Android 7.0 (API 24).

For an icon to be colored by Android, you have to use specific resources for different densities and add them to the appropriate directories.

In my application, I have added icons to

/ Resources / extractor-hdpi

/ Resources / draw-xhdpi



/ Resources / draw-xxhdpi

If you, like mine, have an image for the icon, you can create new assets using this tool: https://romannurik.github.io/AndroidAssetStudio/icons-notification.html

The original code doesn't change, but when you customize icons for different densities, the icon turns colored in the drawer and white in the notification area.

Previously, I just used the default .png icon in the Resources / drawable folder. This displays the icon, but it always displays the same color.

+1


source







All Articles