Hide time in Android notification without using custom layout - android

In the bottom right corner of a standard Android notification is time (eg 12:00). Can I hide it without using custom notification layout?

For API level 17 and onward, the above accepted answer by Erwan is correct.
For historical purposes, the answer below remains
It is possible to do that.
Try setting when to 0.
For the few notifications that don't do that, it looks like thats what they do.
Here's the general API reference.
Here's a link to the ADB notification and search for private void updateAdbNotification()...

From API Level 17 onwards, you can use setShowWhen(false)
public Notification.Builder setShowWhen (boolean show)
Added in API level 17 Control whether the timestamp set with setWhen
is shown in the content view.
http://developer.android.com/reference/android/app/Notification.Builder.html#setShowWhen(boolean)

For API level 17 and onward,
mBuilder.setShowWhen(boolen);
1.Use Hide Notification Time
mBuilder.setShowWhen(false);
2.Use Show Notification Time
mBuilder.setShowWhen(true);

Related

How to manage setNumber or setSubText method of Notification in Android Nougat

As android google blog clearly mentioned that Android Nougat is not supporting setNumber and setContentInfo at all and even I have tested it on Android Nougat.
Line from Google blog:
In addition, the subtext now supersedes the role of content info and
number
So when I use setNumber for devices prior than Nougat and setSubText for Nougat then Nougat works perfectly only setSubText method works. But when I run it on Devices running prior versions they run both methods as setNumber and setSubText.
So how I can handle this?
Why android prefer setSubText?
In addition what is the difference between setNumber and setContentInfo ?
So how I can handle this?
Your desired result is to have your notification display whatever you pass to setSubText() on Nougat and above and whatever you pass to setNumber() on Marshmallow and below. You can accomplish this by checking the Build info at runtime:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// your setSubText() code here
} else {
// your setNumber() code here
}
Why android prefer setSubText?
I can only really quote the blog post you linked here. They say:
"Many of the fields that were spread around the notifications have been collapsed into a new header row with your app’s icon and name anchoring the notification."
It sounds to me like a design-based preference.
In addition what is the difference between setNumber and setContentInfo ?
Documentation for setContentInfo(): "Set the large text at the right-hand side of the notification."
Documentation for setNumber(): "Set the large number at the right-hand side of the notification. This is equivalent to setContentInfo, although it might show the number in a different font size for readability." (emphasis added)

Show PopupWindow above all window for target android 26

I want to show PopupWindow above all window (example: another popupwindow, dialog, another activity screen) without request SYSTEM_ALERT_WINDOW so I use WindowManager.LayoutParams.TYPE_TOAST
public void showSimplePopupWindow() {
final View popupView = layoutInflater.inflate(R.layout.popup_layout_2, null);
final PopupWindow popupWindow = new PopupWindow(popupView);
...config popup window...
PopupWindowCompat.setWindowLayoutType(popupWindow, WindowManager.LayoutParams.TYPE_TOAST);
popupWindow.showAsDropDown(findViewById(R.id.button_show_popup_window));
}
It working well in all android version if I set targetSdkVersion < 26 .
Currently, If I keep the code above and update the target targetSdkVersion to 26 then it will crash with device api 25-26 with exception android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W#859d91f is not valid; is your activity running?
I see that TYPE_TOAST is deprecated in sdk 26 and they suggest to use TYPE_APPLICATION_OVERLAY. However, when I use TYPE_APPLICATION_OVERLAY, AndroidStudio show TYPE_APPLICATION_OVERLAY required api 26. Therefore, TYPE_APPLICATION_OVERLAY only work well with device api 26, for device api < 26, it will crash ( even I have enabled Display/Draw over other app permission)
Is there any alternative way to make TYPE_TOAST work with target api 26? Any help or suggestion would be great appreciated.
There appears to be a bug with API 25. The bug is that if you change the target API to be 26, API 25 devices will no longer be able to use the TYPE_TOAST parameter, even though it is allowable in API 25.
The only way to fix this behavior is to have the user enable overlay permissions in Settings for your app. API 25 and 26 overlays will then function as expected.
Since:
TYPE_APPLICATION_OVERLAY (introduced in Api level 26) is a constant value 2038 (0x000007f6)
TYPE_TOAST (since Api level 1!) is a constant value 2005 (0x000007d5)
one option would be to check the System Version at Runtime and, depending on its value, using the right constant.

How do I make make my notification use default application colours?

I am following this tutorial on notifications, however, my notification is greyed out.
I know there's remote views to style it, but this developers guide does not use it.
Here is an example of how my notification looks
In your builder, use .setColor()
it requires a minimum api
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setColor(Color.BLUE);
}

Custom Big Notification In API level 10

I'm using a Custom View for notifications.
In this case, I know that for a big size notification, I have to use bigContentView and set a big view for it.
Every thing is good in API 16, But my app minimum SDK is API level 10 and bigContentView is not supported in API level 10.
myNotification= notification.setContentTitle("some string")
.setContentText("some text")
.setSmallIcon(R.mipmap.ic_launcher)
.build();
RemoteViews notificationView = new RemoteViews(MyApp.getAppContext().getPackageName(),
R.layout.custom_notification_layout);
myNotification.bigContentView=notificationView; //Error in this line!
Is there another way to display big layout for a custom notification?
Or is a older version of bigContentView?
Big view for notifications were introduced in and android 4.1 and they're not supported on older devices. (as #Pop Tudor commented)
We can use it by bigContentView(RemteView) for notifications if api level is upper than 15.
In API 15 or lower, we can only set custom view for notifications by contentView(View) method.

How to display timestamp in Android Notifications for Honeycomb+ devices?

I'd like to show a timestamp in an Android Notification, just like the Android Design Guidelines suggest. (see the first snapshot, "12:03PM" is what I want!).
I tried many different ways, I thought setWhen would do it, but it only seems to affect the ordering in the Notification tray. Any idea how to achieve that?
See my code below:
Notification.Builder builder = new Notification.Builder(context);
builder.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_notify)
.setLargeIcon(largeIcon)
.setTicker(text)
.setNumber(unreadCount)
.setWhen((new Date()).getTime())
.setAutoCancel(true)
.setContentTitle(title)
.setContentText(text)
.setDefaults(Notification.DEFAULT_VIBRATE);
notificationManager.notify(NOTIFICATION_ID, builder.getNotification());
I don't want to use a custom layout for the notification.
I think I found the answer myself, when seems to be displayed only on Android 4.0.3 and later.
My wife's Nexus S (4.0.3) shows it, and I just upgraded to 4.0.4 on my Galaxy Nexus and it magically started to work!
It also shows on older versions (2.3 for instance), but not on 4.0.2 !
Use setContentInfo and pass your date as a string into it.
From this link
public Notification.Builder setContentInfo (CharSequence info) Since: API Level 11
Set the large text at the right-hand side of the notification.
Large Text at the right hand side of the notification should be where you want to put time.
Also from the same link:
public Notification.Builder setWhen (long when) Since: API Level 11
Set the time that the event occurred. Notifications in the panel are
sorted by this time.
which means that setWhen only sorts the notifications, and doesn't set any text.

Categories

Resources