I've made a custom RemoteView in JellyBean, as described here, and set it as the bigContentView of a notification.
notification.bigContentView = customNotifView;
I'm trying to have the custom layout placed below the standard contentView after the expansion; something like this.
The problem is that the custom layout overrides the standard contentView after the expansion.
Is there a way to do that?
I solved by creating a custom layout for the contentView called layout_base_content_notification.xml, which is exactly the same (handmade) layout Android provides for notifications.
RemoteViews collapsedViews = new RemoteViews(c.getPackageName(), R.layout.layout_base_content_notification);
Notification noti = builder.build();
noti.contentView = collapsedViews;
Then I've included it in a customLayout, called layout_big_content_notification.xml:
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/layout_base_content_notification" />
And added it as a bigContentView:
RemoteViews expandedViews = new RemoteViews(c.getPackageName(), R.layout.layout_big_content_notification);
noti.bigContentView = expandedViews;
Now, after the expansion, the bigContentView replaces the contentView, but their header is the same.
Please let me know if there is a better solution.
Much simple solution would look like this
RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.big_notificaiton_layout);
fillTextViews(profile, contentView);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NotificationChannelHelper.Channel.Channel.getId())
.setContentIntent(intent)
.setContentTitle(context.getResources().getString(R.string.profile_name_is_active, profile.getTitle()))
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setCustomBigContentView(contentView)
.setOngoing(true)
.setSmallIcon(R.drawable.icon);
You have to set NotificationCompat.DecoratedCustomViewStyle().
You need to create your own RemoteViews, then indicate that you want the expanded content to inherit your custom RemoteViews.
RemoteViews expandedView = new RemoteViews(YOUR CONTEXT.getPackageName(), YOUR CUSTOM LAYOUT);
Notification notification = mBuilder.build();
notification.bigContentView = expandedView;
or check this link.
Related
How to remove 'now' and the drop-down arrow from notification layout in my app. I am using a single layout for notification and yet it is expanding. I kept the height to 64dp and 50dp but no change in the size
// Get the layouts to use in the custom notification
RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_small);
//RemoteViews notificationLayoutExpanded = new RemoteViews(getPackageName(), R.layout.notification_large);
notificationLayout.setTextViewText(R.id.otpText,otp);
// Apply the layouts to the notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setSmallIcon(R.drawable.ic_launcher_foreground)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(notificationLayout)
.setPriority(PRIORITY_MAX)
.setDefaults(DEFAULT_SOUND)
.setColor(getResources().getColor(R.color.colorAccent))
.setDefaults(DEFAULT_VIBRATE)
.setContentIntent(pendingIntent);
just use
builder.setSmallIcon(R.drawable.ic_launcher_foreground)
...
.setWhen(0)
...
According to the Documentation you can use:
setWhen(long when)
setShowWhen(boolean show);
By default setShowWhen is true, so you can set it to setShowWhen(false) to remove the timestamp.
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).
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.
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);
I am creating a custom notification using remoteviews. It works great, but if another notifications arrive then my custom notification gets contracted.
Is there any way to always have it expanded?
I use this code:
Notification.Builder builder = new Notification.Builder(MyContext);
builder.setTicker("My ticker");
... other builder values ...
RemoteViews oRemoteViews = new RemoteViews(Contexto.getPackageName(), R.layout.my_notification_layout);
oRemoteViews.setImageViewResource(R.id.myIcon, R.drawable.myicon);
oRemoteViews.setTextViewText(R.id.text1, "Other text here");
... other views values ...
Notification oNotification = builder.build();
oNotification.bigContentView = oRemoteViews;
NotificationManager oNotificationManager = (NotificationManager) Contexto.getSystemService(Context.NOTIFICATION_SERVICE);
oNotificationManager.notify("xxxxx", NOTIF_ID, oNotification);
According to the code below, I create a notification which is expandable.
private void bannerNotif() {
Notification foregroundNote;
RemoteViews bigView = new RemoteViews(getApplicationContext()
.getPackageName(), R.layout.banner_notif);
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.drawable.featuredimagehandler);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
this);
foregroundNote = mNotifyBuilder.setContentTitle("Title")
.setContentText("Description")
.setSmallIcon(R.drawable.app_icon).setLargeIcon(icon).build();
foregroundNote.bigContentView = bigView;
NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyManager.notify(2, foregroundNote);
}
According to this document
A notification's big view appears only when the notification is expanded, which happens when the notification is at the top of the notification drawer, or when the user expands the notification with a gesture.
the problem is when this notification is placed on top, it is not expanded.
I had this problem and the issue was that the layout I created for the notification had a height of "MATCH_PARENT" so it was too big to fit. Setting the height to a reasonable value solved the issue and the notification expanded.
I also had something similar with remoteviews and notifications. Mine ended up not expanding because my notification_id was not unique throughout my app :)
I am trying to create a notification with custom RemoteView.But when displaying notification,bottom of the RemoteView is getting cropped.Only half of the remote view is visible in the notification.Could any one tell me about increasing the height of notification in android?
First of all, Notification height is limited to 256dp on Android. Views will be clipped at the bottom that exceed this height. Second, to create a custom Big Notification layout, use RemoteViews like this:
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_notification_icon)
RemoteViews content = new RemoteViews(this.getPackageName(), R.layout.content);
Notification notif = builder.build();
notif.bigContentView = content;
notificationManager.notify(NOTIFICATION_ID, notif);
Set the style of the notification intent to bigstyle,
Notification noti = new Notification.Builder(this).setStyle(new Notification.BigTextStyle().bigText(longText))