45

I find myself curious why the setLargeIcon method on Notification.Builder only accepts a Bitmap, with no overload to provide a resource id. Perhaps it was done for performance reasons, but it seems odd as setSmallIcon does accept a res drawable id.

Notification.Builder builder = new Notification.Builder(application);
// ....
builder.setLargeIcon(iconBitmap);  // Requires a Bitmap
builder.setSmallIcon(iconResId);   // Requires a drawable resource ID
Notification notification = builder.getNotification();

Sadly the bitmap provided is not scaled in the notification, so the Bitmap needs to be provided exactly the right size for the notification view.

Assuming I need to provide xhdpi, hdpi, mdpi and ldpi versions of the largeIcon bitmap, what sizes do they need to be? I can see no mention in the docs, or after scouring the wider web.

Ollie C
  • 28,313
  • 34
  • 134
  • 217

2 Answers2

62

Not had a chance to check it yet but API 11 introduced the following public dimens:

Should be able to use those to scale your image before setting it on the notification.

crafty
  • 2,730
  • 1
  • 21
  • 10
  • Ah, that's interesting. Do you know what units those values are in (17104902, 17104901)? – Ollie C Aug 28 '11 at 19:34
  • 5
    They are most likely dp's in the xml. When you use Resources. getDimensionPixelSize() you'll get them in px. – alexanderblom Aug 28 '11 at 20:19
  • 5
    Thanks guys. This does the trick nicely. I figure the reason it requires a Bitmap not a resId is it's designed for social media use, and peoples' faces won't typically be distributed with the app. width = res.getDimensionPixelSize(android.R.dimen.notification_large_icon_width) – Ollie C Aug 29 '11 at 12:34
  • 4
    That's 64dp x 64dp https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/dimens.xml#L150 – Jeroen Feb 05 '13 at 12:18
  • 64 dp x 64 dp is also supported by [this answer](http://graphicdesign.stackexchange.com/a/15778/13327) on Graphic Design – JesperB Jun 12 '13 at 07:31
  • 6
    So the best solution is: `int width = getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_width);` `int height = getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_height);` – Paul Woitaschek Feb 24 '15 at 13:46
61

I used the dimensions of the notification's large icon to create a scaled bitmap

BitmapDrawable contactPicDrawable = (BitmapDrawable) ContactsUtils.getContactPic(mContext, contactId);
Bitmap contactPic = contactPicDrawable.getBitmap();

Resources res = mContext.getResources();
int height = (int) res.getDimension(android.R.dimen.notification_large_icon_height);
int width = (int) res.getDimension(android.R.dimen.notification_large_icon_width);
contactPic = Bitmap.createScaledBitmap(contactPic, width, height, false); 

And then I set the large icon with this scaled bitamp.

Etienne Lawlor
  • 6,817
  • 18
  • 77
  • 89
  • 1
    This answer completes the whole picture. Thanks! – iHearGeoff Jan 29 '13 at 04:34
  • You might consider storing the result into a new variable Bitmap scaledContactPic and adding if(!contactPic.equals(scaledContactPic)){ contactPic.recycle(); } – malinjir Jun 20 '17 at 14:20