NotificationCompat 4.1 SetSmallIcon and SetLargeIcon

65,221

Solution 1

  1. In my application I provide large (128x128 px) PNG drawable as small icon, and it shows scaled and without cropping. Is your drawable defined in bitmap file or maybe as XML resource? In XML you can specify several aspects of display (e.g. cropping). Double check your XML or use just PNG/JPG.

  2. As Android API documentation on Notification.setSmallIcon() clearly states:

    Set the small icon resource, which will be used to represent the notification in the status bar. The platform template for the expanded view will draw this icon in the left, unless a large icon has also been specified, in which case the small icon will be moved to the right-hand side.

AFAIK there's no way you can override the behaviour, unless you provide your own notification template (via Notification.setContent()

Solution 2

There is a way around this weird implementation. Instead of using setLargeIcon use this:

Notification notification=notificationBuilder.build()
notification.contentView.setImageViewResource(android.R.id.icon, R.drawable.your_large_icon);

Solution 3

I'd guess that this is the expected behavior.

You should check to see that your small icon follows the UX guidelines for icon size. Small icons are limited to 24x24dp.

The default behavior of an expanded notification is to show both the large icon and the small icon. I'm not sure that there's a way to get rid of the small icon, but why is this important?

Solution 4

In my case, I just set my red icon as the large icon and the setColor to Color.WHITE and set a white icon as my small icon. That way, in the notifications area, my red icon is shown and the white icon is "disapeared".

Bitmap icon = BitmapFactory.decodeResource(getResources(),R.drawable.app_logo);

mBuilder.setContentIntent(resultPendingIntent).setColor(Color.WHITE).setLargeIcon(icon);
Share:
65,221
Ton
Author by

Ton

Updated on June 02, 2020

Comments

  • Ton
    Ton almost 4 years

    I used this simple code to set a Notification in Android 4.1 or higher. It works well, but my problem comes with SmallIcon and LargeIcon. I understand that SmallIcon is shown in the status bar and the LargeIcon is shown in the dropdown list.

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setTicker("The ticker");
    builder.setContentTitle("The title");
    builder.setContentText("The text");
    builder.setSmallIcon(R.drawable.my_small_icon);
    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.my_96px_large_icon);
    builder.setLargeIcon(bm);       
    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    nm.notify("direct_tag", NOTIF_ALERTA_ID, builder.build());
    

    My problem is:

    1. When the notification is launched, a cropped oversized Small Icon is shown next to "The Ticker" text, instead of showing the original SmallIcon without oversizing it. enter image description here

    2. In the dropdown list I see the LargeIcon on the left, that's good. But I also see the small icon on the right, next to the time of the notification. I don't want to show that. enter image description here

  • Ton
    Ton over 11 years
    It is important because my application is the only one showing that small icon next to the time.
  • Admin
    Admin about 11 years
    I fixed this in my app by using the same LARGE icon that I have all this time, and scaling my SMALL icon down to 24x24dp (e.g. 24 by 24 pixels). Thank you so much for this size note, as I see it nowhere else.
  • Ruchir Baronia
    Ruchir Baronia over 8 years
    Where are the UX guidlines?
  • GregD
    GregD almost 8 years
    Nice trick. It bypasses the machinery for small and large icon interaction and just modifies the imageview after the notification is built.
  • Ego Slayer
    Ego Slayer about 7 years
  • Swindler
    Swindler almost 7 years
    Deprecated. But apparently you can use NotificationCompat.Builder.setCustomContentView(RemoteViews) instead.