2

I'm trying to display a notification with expanded layout on Lollipop, but i don't want the smallIcon to show up on the bottom right corner of the largeIcon as per the example below. notice that this only happens for the extended layout (i tried the solution proposed here, but it doesn't seem to work). enter image description here

here is the code:

NotificationCompat.Builder notification = new NotificationCompat.Builder(context);
notification.setContentTitle(reader.getString(Constants.NOTIFICATION_CONFIG_KEY_TITLE));
notification.setContentText(reader.getString(Constants.NOTIFICATION_CONFIG_KEY_TEXT)) ;
notification.setAutoCancel(true) ;
notification.setSmallIcon(Constants.NOTIFICATION_CONFIG_ICON_ID);
notification.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), Constants.NOTIFICATION_CONFIG_ICON_ID));
Notification n = notification.build() ;

the smallIcon's visibility could probably be changed once the notification is built but i don't have a clear idea how. perhaps, someone could help with that.

Thanks.

Community
  • 1
  • 1
Paghillect
  • 822
  • 1
  • 11
  • 30

1 Answers1

0

You can achieve that by building both layouts (the short and the expand) by yourself.

First make 2 XML files and custom your layouts, then attach them to the notification. The root view of those layouts should be LinearLayout:

RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_layout); // the small layout

RemoteViews notificationLayoutExpanded = new RemoteViews(getPackageName(), R.layout.notification_layout_large); // the expand layout

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, getString(R.string.notification_channel_id))
                .setSmallIcon(R.drawable.logo)
                .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
                .setCustomContentView(notificationLayout)
                .setCustomBigContentView(notificationLayoutExpanded);


NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        notificationManager.notify(0, builder.build());

If you want to set a text or an image to those layouts dynamically (not in the XML), you do it like that:

RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_layout);
        
String text = "the text to display";
Bitmap picture = BitmapFactory.decodeResource(getResources(), R.drawable.notification_default_img);;


notificationLayout.setTextViewText(R.id.small_notification_text, text); 
notificationLayout.setImageViewBitmap(R.id.small_notification_img, picture);
Avital
  • 549
  • 5
  • 15