Why do icons set with Notification.Builder.setSmallIcon in Android Lollipop show as a white square?

AndroidNotificationsAndroid 5.0-LollipopAndroid Notification-Bar

Android Problem Overview


I have this code:

Notification notif;

// Build notification
Notification.Builder notifBuilder = new Notification.Builder(context);
notifBuilder.setContentIntent(pendingIntent);
notifBuilder.setContentTitle(title);
notifBuilder.setSmallIcon(icon_resId);
notifBuilder.setContentText(ne.getCaption());
notifBuilder.setDefaults(Notification.DEFAULT_ALL);
notifBuilder.setAutoCancel(autocancel);
notifBuilder.setWhen(System.currentTimeMillis());
notif = notifBuilder.build();

and works fine in Android 4.4.

However, in Android 5.0 the icon showed in status bar is a white square. The icon showed in the new "notification body", that appears when device is locked, is correct.

In http://developer.android.com/reference/android/app/Notification.Builder.html, I don't see anything new about notification icons in API Level 21

Android Solutions


Solution 1 - Android

Look at the documentation: http://developer.android.com/design/style/iconography.html

there are words: "Notification icons must be entirely white. Also, the system may scale down and/or darken the icons."

Solution 2 - Android

I have resolved changing the icon size to 16x16 px and using only white color

Solution 3 - Android

As noted in Android 5.0 Behavior Changes of the Android Developers site under Notifications:

Notifications are drawn with dark text atop white (or very light) backgrounds to match the new material design widgets. Make sure that all your notifications look right with the new color scheme. If your notifications look wrong, fix them:

Use setColor() to set an accent color in a circle behind your icon image. Update or remove assets that involve color. The system ignores all non-alpha channels in action icons and in the main notification icon. You should assume that these icons will be alpha-only. The system draws notification icons in white and action icons in dark gray.

http://developer.android.com/about/versions/android-5.0-changes.html.

Solution 4 - Android

Duplicate : https://stackoverflow.com/questions/28387602/notification-bar-icon-turns-white-in-android-5-lollipop

In a Brief :

Android 5 update : https://developer.android.com/about/versions/android-5.0-changes.html Notifications -> Material design style > Update or remove assets that involve color. The system ignores all > non-alpha channels in action icons and in the main notification icon. > You should assume that these icons will be alpha-only. The system > draws notification icons in white and action icons in dark gray.

It's possible to set the small icon background color using (default is gray) :

Notification.Builder#setColor(int)

Solution 5 - Android

Add this in your manifest -

 <meta-data android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@drawable/ic_notification" />

Solution 6 - Android

Anyone still looking at this, the simplest way of getting your icon to display correctly is first rendering it with the Android Icon Studio here:

<https://romannurik.github.io/AndroidAssetStudio/icons-notification.html>

Unzip the files from the downloaded zip into your project /main folder, so they slot into the relevant drawable-xxxx folders.

Then, to change colour in the notification use something like this:

NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
    .setSmallIcon(R.drawable.ic_notification_appicon) // <-- Icon from Android Icon Studio 
    .setColor(context.getColor(R.color.holo_blue))    // <-- Set your preferred icon colour to appear in the notification dropdown list
    .setContentTitle("Title")
    .setContentText("Content")
    .setAutoCancel(true)
    .setCategory(NotificationCompat.CATEGORY_EVENT)
    .setDefaults(Notification.DEFAULT_ALL)
    .setPriority(NotificationCompat.PRIORITY_DEFAULT);

Solution 7 - Android

In Android 5.0 the icon showed in the status bar is a white square because of 5.0 Lollipop "Notification icons must be entirely white".

You can easily find this types of icons on the Material icon. Visit: https://material.io/icons/

Google also suggests that we use a custom color that will be displayed behind the white notification icon using setColor() method.

For more information visit: https://developer.android.com/about/versions/android-5.0-changes.html

Solution 8 - Android

Remove the android:targetSdkVersion="21" from manifest.xml. it will work!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBorjaView Question on Stackoverflow
Solution 1 - AndroidporlicusView Answer on Stackoverflow
Solution 2 - AndroidBorjaView Answer on Stackoverflow
Solution 3 - AndroidCoDeSignsView Answer on Stackoverflow
Solution 4 - AndroidTobliugView Answer on Stackoverflow
Solution 5 - AndroidA JView Answer on Stackoverflow
Solution 6 - AndroidJames SemajView Answer on Stackoverflow
Solution 7 - AndroidAashish KumarView Answer on Stackoverflow
Solution 8 - Android范文锋View Answer on Stackoverflow