Android remove smallIcon overlay from expanded notification layout - android

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);

Related

How to set or change notification buttons position

Can not find any information about buttons positioning. I have discovered that different devices show buttons in different (left\center\right)
I can provide some code from my Xamarin project but Kotlin\Java code should be almost the same
var notificationBuilder = new NotificationCompat.Builder(Application.Context, ChannelId)
.SetSmallIcon(ApplicationContext.ApplicationInfo.Icon)
.SetContentTitle("title")
.SetContentText("message")
.SetAutoCancel(true)
.SetStyle(new NotificationCompat.BigTextStyle().BigText(message))
.SetColor(Resource.Color.orange)
.SetContentIntent(contentIntent);
var buttonIntent = new Intent(ApplicationContext, typeof(NotificationReceiver));
buttonIntent.PutExtra(NotificationConstants.SelectedActionKey, selectedAction);
var pendingIntent = PendingIntent.GetBroadcast(ApplicationContext, requestCode, buttonIntent, PendingIntentFlags.UpdateCurrent);
notificationBuilder.AddAction(0, "cool button", pendingIntent),
You won't be able to pick how the default actions are displayed. But you can instead custom view.
RemoteViews remoteViews = new RemoteViews("com.companyname.NotificationChannelsDemo", Resource.Layout.layout1);
var builder = new NotificationCompat.Builder(this, CHANNEL_ID2)
.SetCustomContentView(remoteViews).
.........
com.companyname.NotificationChannelsDemo is your package name.
Resource.Layout.layout1 is the layout of your custom view. You could set the button position in this layout.

how to remove time counter from notification?

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.

Android notification: the top notification is not expanded

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 :)

How to increase the height of remoteview in android notification

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))

Android's Expanded Notifications - Both Standard ContentView and custom BigContentView after expansion

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.

Categories

Resources