John Sardinha John Sardinha - 1 year ago 82
Java Question

Notification large icon looking small

I'm trying to set a drawable in a notification's large icon, but a strange behaviour is happening, the drawable gets smaller for some reason, it's the first notification in this image:

enter image description here

This is the drawable:

<layer-list xmlns:android="" >
<shape android:shape="rectangle" >
<solid android:color="@color/subject7"/>
<corners android:radius="30dp"/>
<item android:drawable="@drawable/calendar_clock_colored_clicked" />

This is how I'm setting the largeIcon:

notification = new NotificationCompat.Builder(context);
notification.setLargeIcon(drawableToBitmap(ResourcesCompat.getDrawable(context.getResources(), drawableID, null)));


public Bitmap drawableToBitmap (Drawable drawable) {

if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable)drawable).getBitmap();

int width = drawable.getIntrinsicWidth();
width = width > 0 ? width : 1;
int height = drawable.getIntrinsicHeight();
height = height > 0 ? height : 1;

Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());

return bitmap;

Has this happened to anyone?

Edit: This is in a BroadcastReceiver, calling itself every 60 seconds to refresh the notification, the first time the largeIcon appears properly, it only appears smaller after the first time.

Answer Source

In case someone stumbles into this.

Make the bitmap with an actual PNG, with the proper padding into it, like this:

private Bitmap getBitmap(int color) {
    Drawable drawable = ResourcesCompat.getDrawable(context.getResources(), R.drawable.notification_class_icon, null);
    Canvas canvas = new Canvas();
    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);

    //set round background on lolipop+ and square before
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Paint p = new Paint();
        p.setColor(ContextCompat.getColor(context, color));
        canvas.drawCircle(canvas.getHeight()/2, canvas.getHeight()/2, canvas.getHeight()/2, p);
    else {
        canvas.drawColor(ContextCompat.getColor(context, color));
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

    return bitmap;

This is R.drawable.notification_class_icon looks:

enter image description here (the black is suposed to be transparent).

Make sure you make one for every dimension.

Then just set the notifications' icon like this and pass the color for the background: