Reputation: 5096
I want to show constantly small icon in the status bar
, without show the notification itself on the notification
tray of the system.
I tried using custom layout
and set the visibility of the root element to View.GONE
, or View.INVISIBLE
, but for both, the system ignores it.
I also tried to set the height of the root element but the system ignores this too.
Here is my code:
R.layout.custom_notification
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/custom_notification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/notif_background"
android:orientation="horizontal"
android:baselineAligned="false"/>
And the code for display the Notification
:
NotificationManager nm = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctx);
mBuilder
.setSmallIcon(drawableId)
.setAutoCancel(false)
.setOngoing(true)
.setPriority(Notification.PRIORITY_MAX);
Intent resultIntent = new Intent(ctx, SettingsActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(ctx, 0, resultIntent, 0);
mBuilder.setContentIntent(resultPendingIntent);
int layout = R.layout.custom_notification ;
RemoteViews contentView = new RemoteViews(ctx.getPackageName(), layout);
contentView.setViewVisibility(R.id.custom_notification, View.INVISIBLE);
mBuilder.setContent(contentView);
nm.notify(NOTIFICATION_ID, mBuilder.build());
I'm well aware that for achieving my goal I will need not documented solution. And I am also aware that UX guidelines strongly recommending to not do such a thing. So there is no point in writing me this as an answer.
Upvotes: 22
Views: 8058
Reputation: 188
Late but maybe useful to others.
In my case its so simple just use height 0dp in your main custom layout like
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="0dp">
wrap_content will not work here.
Upvotes: 0
Reputation: 1
you can use RemoteViews to hide title and text
R.id.notification_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_marginRight="10dp"
android:layout_marginLeft="7dp"
android:src="@drawable/icon_battery" />
</RelativeLayout>
in MainActivity:
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_layout);
Context application = getApplicationContext();
Intent resultIntent = new Intent(application, MainActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultPendingIntent = PendingIntent.getActivity(application, 0, resultIntent, 0);
NotificationManager nmgr = (NotificationManager) application.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(application)
.setSmallIcon(R.drawable.icon_battery);
mBuilder.setContent(contentView);
Notification mNotification = mBuilder.build();
// mNotification.flags |= FLAG_NO_CLEAR;
nmgr.notify(0, mNotification);
Upvotes: 0
Reputation: 6181
It is not good idea to show an icon without show him any related Notification, As per developer docs,
A status bar notification requires all of the following:
So if you don't want to ruin the User Experience then it's highly recommendable to follow UX guidelines,
logically: There are two parts of status bar – user (notifications) and system (other). If system allows to put something to system part – there will be so much useless icons (posted by other applications), and there will be no consistency, and it might break all of the use experience.
Found an answer which provide you way to access default status bar icon of Alarm it is here. After Root your mobile you can do it.There are some tricks on youtube doing it, Some answers on same topic suggest to show icon using Notification but put Notification Text empty so that user will only see icon, but again it's not a way.
This is code for Status bar implementation in System where Left and Right Icons(i.e System Icons) are described may you can get some lead from there. Answer that describe it is here.
Upvotes: 9
Reputation: 27211
I would suggest do not use for usuall Google Play application. There are several reasons:
If you find a bug which can help to show invisible notification it does seem nothing. The bug can work only in partical device with the particular Android version. Remember: this is very-very hard to setup your regular code be correctly executed in most of devices using opened API. Hidden API doesn't work in all devices.
There is no such behaviour in the source code: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/app/NotificationManager.java#NotificationManager.notify%28int%2Candroid.app.Notification%29 . You can find nothing with such behavoiur.
To predictable behaviour use *Compat classes of Android. That is, for notification use only NotificationCompat
class! Only with wrapper can help your app to show notifications.
This is very very ugly UI/UX.
Notifications' workflow is dramatically depend on Android version.
Upvotes: 6
Reputation: 1358
You can do this using InputMethodService.showStatusIcon(). But I suggest you to display normal notification and explain to user what your app do right now.
Upvotes: 1