NotificationCompat android - how to show only Large icon without small - android

When I add notification:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.plus)
.setContentTitle(title)
.setAutoCancel(true)
.setContentText(text)
.setSound(RingtoneManager .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setLargeIcon(bm);
I see large icon and small in it:
How can I set only large Icon, without small.
If use only setLargeIcon, I don't see notification at all, just sound alert.

Small icon is mandatory.
If you don't set a large one you'll get your small icon bigger in the middle of the circle with the color of your choice (setColor).
If i were you i'd put that blank E on a transparent background for smallicon, and set a red color for the circle.

get the small icon id and then try to hide it
int smallIconId = ctx.getResources().getIdentifier("right_icon", "id", android.R.class.getPackage().getName());
if (smallIconId != 0) {
if (notification.contentView!=null)
notification.contentView.setViewVisibility(smallIconId, View.INVISIBLE);
}
try looking at this post it will help too
i test the code on api 18,23 (samsung j1,galaxy S6) work fine

Based on the previous answer you can also hide the expended view too :
int smallIconId = AnghamiApp.getContext().getResources().getIdentifier("right_icon", "id", android.R.class.getPackage().getName());
if (smallIconId != 0) {
notification.contentView.setViewVisibility(smallIconId, View.INVISIBLE);
notification.bigContentView.setViewVisibility(smallIconId, View.INVISIBLE);
}

You can create a custom notification and then show whatever you want in the large notification area.
See and example here TutorialsFace: Build all types of Notifications in Android

public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("Test")
.setContentText("Hii There")
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.smallicon))
.setAutoCancel(true)
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(123, notification);
}
}

Related

How to use a drawable with adjustable text as notification action icon

What I need
What I want to achieve is something like
where it should be possible to programmatically set the number displayed within the icon.
I.e. I want a notification action icon with a programmatically adjustable text.
Is there a simple way to achieve this? It does not seem to be possible to add a TextView to an android drawable and set the text when I'm building my notification. What other ways are there?
Code
The rough code for building my notification is (although I think it's not necessary for answering the question):
private void buildNotification() {
createNotificationChannel();
Intent playbackActionIntent = new Intent(this, MediaPlayerService.class);
playbackActionIntent.setAction(ACTION_BACKWARD);
PendingIntent backwardAction = PendingIntent.getService(this, 3, playbackActionIntent, 0);
String audioTitle = "testTitle";
String albumTitle = "testAlbumTitle";
// Create a new Notification
mNotificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
// Hide the timestamp
.setShowWhen(false)
// Set the Notification style
.setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle()
// Attach our MediaSession token
.setMediaSession(mediaSession.getSessionToken())
// Show our playback controls in the compat view
.setShowActionsInCompactView(0, 1, 2))
// Set the Notification color
.setColor(getResources().getColor(R.color.colorAccent))
.setSmallIcon(R.drawable.ic_notification_new)
// Set Notification content information
.setContentText(albumTitle)
.setContentTitle(audioTitle)
// Set the visibility for the lock screen
.setVisibility(VISIBILITY_PUBLIC)
// Add playback actions
.addAction(R.drawable.ic_media_backward, "backward", backwardAction)
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.notify(NOTIFICATION_ID, mNotificationBuilder.build());
}
}
So for R.drawable.ic_media_backward I want a drawable with an adjustable text as in the linked image.
Any help is appreciated! So far I could not find anything that would solve my problem.

App crashes on API level 23 : Exception java.lang.IllegalArgumentException: Invalid notification (no valid small icon):

I am displaying a custom view notification, it is working perfect except in marshmallow ,while i am using 24x24 ,36x36, 48x48, 72x72 px icon
android.support.v7.app.NotificationCompat.Builder builder = new android.support.v7.app.NotificationCompat.Builder(m_context);
Intent i = new Intent(m_context, RunningAppsActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(m_context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setTicker(m_context.getResources().getString(com.soopermo.batterybooster.R.string.click_to_optimize));
builder.setSmallIcon(R.drawable.battry_notify);
builder.setAutoCancel(true);
Notification notification = builder.build();
RemoteViews contentView = new RemoteViews(m_context.getPackageName(),R.layout.status_bar);
// Set text on a TextView in the RemoteViews programmatically.
final String text = m_context.getResources().getString(com.soopermo.batterybooster.R.string.draining_apps) ;
contentView.setTextViewText(com.soopermo.batterybooster.R.id.status_bar_content_desc2, text);
contentView.setOnClickPendingIntent(com.soopermo.batterybooster.R.id.optimization, intent);
notification.contentView = contentView;
// Add a big content view to the notification if supported.
// Support for expanded notifications was added in API level 16.
// (The normal contentView is shown when the notification is collapsed, when expanded the
// big content view set here is displayed.)
if (Build.VERSION.SDK_INT >= 16) {
// Inflate and set the layout for the expanded notification view
RemoteViews expandedView =
new RemoteViews(m_context.getPackageName(), com.soopermo.batterybooster.R.layout.status_bar);
notification.bigContentView = expandedView;
expandedView.setOnClickPendingIntent(com.soopermo.batterybooster.R.id.optimization, intent);
}
NotificationManager nm = (NotificationManager) m_context.getSystemService(NOTIFICATION_SERVICE);
nm.notify(POWER_ISSUES_ID, notification);
//...........................................................
This is happening because above lollypop(21) android doesn't support any colorized image for small icon.
you have to use a image that has white foreground and transparent background.
And use it like this.
private int getNotificationSmallIcon()
{
boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
return useWhiteIcon ? R.drawable.icon_for_above_lollypop : R.drawable.normal_icon;
}
Edit
Here are icons that supports on above 21 https://material.io/icons/
Creating notification icons from Image Asset option would be the good practice. If you are working with vectors, user Vector Asset
Attaching screenshot of my Android studio.
Note: Select notification icons from the dropdown in next screen.

Android remove smallIcon overlay from expanded notification layout

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

Android notification large icon, is there a way to remove the smaller icon on the bottom right?

I have a notification that displays a largeicon.
Is there any way to remove the smaller icon from honeycomb and above devices from this view?
Obviously still keeping the small icon for the top status bar
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
// Set required fields, including the small icon, the
// notification title, and text.
.setSmallIcon(R.drawable.ic_notify_status_new)
.setContentTitle(title)
.setContentText(text)
// All fields below this line are optional.
// Use a default priority (recognized on devices running Android
// 4.1 or later)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Provide a large icon, shown with the notification in the
// notification drawer on devices running Android 3.0 or later.
.setLargeIcon(picture)
.setContentIntent(
PendingIntent.getActivity(
context,
0,
notiIntent,
PendingIntent.FLAG_UPDATE_CURRENT)
);
builder = getNotiSettings(builder);
notify(context, builder.build());
Add this code after you build the notification.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int smallIconViewId = getResources().getIdentifier("right_icon", "id", android.R.class.getPackage().getName());
if (smallIconViewId != 0) {
if (notif.contentIntent != null)
notif.contentView.setViewVisibility(smallIconViewId, View.INVISIBLE);
if (notif.headsUpContentView != null)
notif.headsUpContentView.setViewVisibility(smallIconViewId, View.INVISIBLE);
if (notif.bigContentView != null)
notif.bigContentView.setViewVisibility(smallIconViewId, View.INVISIBLE);
}
}
where "notif" is your built notification,
There is only one way:
Use your custom layout
Your question is probable a duplicate of NotificationCompat 4.1 SetSmallIcon and SetLargeIcon
Use only setSmallIcon for Your icon. It will be also used for largeIcon if second wasn't specified (just make sure it is big enough to fit both).
Make sure that it looks good on pre-lolipop, lolipop and above. For example here I do set application icon only for pre-lolipop devices, where lolipop and above are getting different smallIcon and same largeIcon as smallIcon.
int currentApiVersion = android.os.Build.VERSION.SDK_INT;
if (currentApiVersion >= Build.VERSION_CODES.LOLLIPOP) {
notificationBuilder
.setSmallIcon(smallIcon)
.setLargeIcon(appIconDrawable)
} else {
notificationBuilder.setSmallIcon(appIconDrawable);
}

Android Notification.Builder: show a notification without icon

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.ic_notification_icon;
android.app.Notification.Builder nbuilder = new Notification.Builder(this);
nbuilder.setContentTitle(getString(R.string.notifcation_title,mProfile.mName));
nbuilder.setContentText(msg);
nbuilder.setOnlyAlertOnce(true);
nbuilder.setOngoing(true);
nbuilder.setSmallIcon(icon,level.level);
How can I hide or completely delete the smallIcon? I tried to not use nbuilder.setSmallIcon, but the result is that the notification is not shown at all!
On Jelly Bean and later you can use nbuilder.setPriority(Notification.PRIORITY_MIN); the exact interpretation of this priority level is left up to the system UI, but in AOSP this causes the notification's icon to be hidden.
This is intended for "ambient" or "background" information that doesn't need to get the user's attention, but if the user happens to be poking around the notification panel, she might be interested. See the Notifications section of the Android Design site for more about how to best use notification priority.
Update: don't use it on Android 7, it's crashing
Using reflection, here's something that's working in Lollipop (emulators and Moto G device) and Marshmallow (emulator)
Notification notification = NotificationCompat.Builder.blabla().build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int smallIconViewId = context.getResources().getIdentifier("right_icon", "id", android.R.class.getPackage().getName());
if (smallIconViewId != 0) {
if (notification.contentView != null)
notification.contentView.setViewVisibility(smallIconViewId, View.INVISIBLE);
if (notification.headsUpContentView != null)
notification.headsUpContentView.setViewVisibility(smallIconViewId, View.INVISIBLE);
if (notification.bigContentView != null)
notification.bigContentView.setViewVisibility(smallIconViewId, View.INVISIBLE);
}
}
I couldn't find a better way.
This can stop working the moment they change the id of the view or alter any of the RemoteViews fields inside Notification, so use at your own risk.
you can't do that... without setSmallIcon(icon,level.level); Notification is not showing..
Try in your Notification builder adding this line
.setPriority(Notification.PRIORITY_MIN)
So your Notification will be like this :
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.stat_notify_chat)
.setContentTitle("my app")
.setContentText("my app ")
.setPriority(Notification.PRIORITY_MIN)
.setContentIntent(pendingIntent).build();
So in this Case you can see your icon just in Notification bar and it be hide in status bar . hope this help .
You can set your own layout without icon:
RemoteViews notificationView = new RemoteViews(
context.getPackageName(),
R.layout.notify
);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContent(notificationView)
I faced this problem before and solved it like that:
private int getNotificationIcon() {
boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.LOLLIPOP);
// Lollipop or Marshmellow >>>>>>>>>>>>>>>>>>>>> KitKat or less
return useWhiteIcon ? R.drawable.logo_new : R.drawable.logo;
}
and just call this function in setSmallIcon()
nbuilder.setSmallIcon(getNotificationIcon());

Categories

Resources