The accepted answer is not (entirely) correct. Sure, it makes notification icons show in color, but does so with a BIG drawback - by setting the target SDK to lower than Android Lollipop!
If you solve your white icon problem by setting your target SDK to 20, as suggested, your app will not target Android Lollipop, which means that you cannot use Lollipop-specific features.
Have a look at http://developer.android.com/design/style/iconography.html, and you'll see that the white style is how notifications are meant to be displayed in Android Lollipop.
In Lollipop, Google also suggest that you use a color that will be displayed behind the (white) notification icon - https://developer.android.com/about/versions/android-5.0-changes.html
So, I think that a better solution is to add a silhouette icon to the app and use it if the device is running Android Lollipop.
For instance:
Notification notification = new Notification.Builder(context)
.setAutoCancel(true)
.setContentTitle("My notification")
.setContentText("Look, white in Lollipop, else color!")
.setSmallIcon(getNotificationIcon())
.build();
return notification;
And, in the getNotificationIcon method:
private int getNotificationIcon() {
boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
return useWhiteIcon ? R.drawable.icon_silhouette : R.drawable.ic_launcher;
}
Completely agree with user Daniel Saidi. In Order to have Color
for NotificationIcon
I'm writing this answer.
For that you've to make icon like Silhouette
and make some section Transparent
wherever you wants to add your Colors
. i.e,
You can add your color using
.setColor(your_color_resource_here)
NOTE : setColor
is only available in Lollipop
so, you've to check OSVersion
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Notification notification = new Notification.Builder(context)
...
} else {
// Lollipop specific setColor method goes here.
Notification notification = new Notification.Builder(context)
...
notification.setColor(your_color)
...
}
You can also achieve this using Lollipop
as the target SDK
.
All instruction regarding NotificationIcon
given at Google Developer Console Notification Guide Lines.
Preferred Notification Icon Size 24x24dp
mdpi @ 24.00dp = 24.00px
hdpi @ 24.00dp = 36.00px
xhdpi @ 24.00dp = 48.00px
And also refer this link for Notification Icon Sizes for more info.
This is the code Android uses to display notification icons:
// android_frameworks_base/packages/SystemUI/src/com/android/systemui/
// statusbar/BaseStatusBar.java
if (entry.targetSdk >= Build.VERSION_CODES.LOLLIPOP) {
entry.icon.setColorFilter(mContext.getResources().getColor(android.R.color.white));
} else {
entry.icon.setColorFilter(null);
}
So you need to set the target sdk version to something <21
and the icons will stay colored. This is an ugly workaround but it does what it is expected to do. Anyway, I really suggest following Google's Design Guidelines: "Notification icons must be entirely white."
Here is how you can implement it:
If you are using Gradle/Android Studio to build your apps, use build.gradle
:
defaultConfig {
targetSdkVersion 20
}
Otherwise (Eclipse etc) use AndroidManifest.xml
:
<uses-sdk android:minSdkVersion="..." android:targetSdkVersion="20" />
To avoid Notification icons to turn white, use "Silhouette" icons for them, ie. white-on-transparent background images. You may use Irfanview to build them:
IrfanView
, press F12 for the painting tools, clean picture if necessary (remove unwanted parts, smooth and polish)Image / Decrease Color Depth
to 2 (for a black & white picture)Image / Negative
(for a white on black picture)Image / Resize/Resample
to 144 x 144 (use Size Method "Resize" not "Resample", otherwise picture is increased to 24 color bits per pixel (24 BPP) againFile / Save as PNG
, check Show option dialog
, check Save Transparent Color
, click Save
, then click on the black color in the image to set the transparent colorAndroid seems to be using the drawable-xxhdpi picture resolution (144 x 144) only, so copy your resulting ic_notification.png
file to \AndroidStudio\Projects\...\app\src\main\res\drawable-xxhdpi
. Use .setSmallIcon(R.drawable.ic_notification)
in your code, or use getNotificationIcon()
as Daniel Saidi suggested above.
You may also use Roman Nurik's Android Asset Studio.
Another option is to take advantage of version-specific drawable (mipmap) directories to supply different graphics for Lollipop and above.
In my app, the "v21" directories contain icons with transparent text while the other directories contain non-transparent version (for versions of Android older than Lollipop).
Which should look something like this:
This way, you don't need to check for version numbers in code, e.g.
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_notification)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
Likewise, you can reference "ic_notification" (or whatever you choose to call it) in your GCM payload if you use the "icon" attribute.
https://developers.google.com/cloud-messaging/http-server-ref#notification-payload-support
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With