22

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.

9
  • You want set this small icon when the app start and keep it constantly? Sep 20, 2016 at 7:13
  • Afaik what are you looking for its not possible w/o a permanent notification in the notification area. It's only possible with custom android system so it won't be useful to you
    – Tazz
    Sep 20, 2016 at 7:23
  • @mwisnicki yes, exactly. It's already working great, I just seek for a way to get rid of the notification in the tray and display just the small icon.
    – yshahak
    Sep 20, 2016 at 7:26
  • 1
    @Tazz I am well aware that it is not something easy to solve, as I seek for a way to bypass this limit for more than half a year.
    – yshahak
    Sep 20, 2016 at 7:31
  • 3
    @yshahak You cannot use notifications for that, they are not designed that way, the only solution that comes to mind is to draw over the screen an icon and place it in the notification tray area and calculate how many notifications you receive with a NotificationListener (you need permissions from the user for that) so you move your icon to the right or else it will overlaps that. You also need permissions from the user to draw over screen. Look here for how to draw over other apps: stackoverflow.com/questions/20461979/…
    – Tazz
    Sep 20, 2016 at 7:43

5 Answers 5

9
+75

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:

  • An icon for the status bar (which you are interested in)
  • A title and expanded message for the expanded view (unless you define a custom expanded view)
  • PendingIntent, to be fired when the notification is clicked to show related info.

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.

4
  • Thanks, I am well aware of what you wrote about the need to follow UX guidelines. I wouldn't seek for a solution if I hadn't a good reason to do this. I am working on this app for more than 6 months. Anyway, a solution that involves rooting the phone is not what I am seeking for. Also not a solution of putting empty notification on the tray.
    – yshahak
    Sep 21, 2016 at 10:25
  • yeah i know so i added in last sentence "but again it's not a way." i think it should be in bold .. :) @yshahak
    – TapanHP
    Sep 21, 2016 at 10:44
  • Thanks again. I read carefully the question and the answer you refer to, and although it interesting, it is not the direction I need, as my app is not system app, and I have no access to the source code.
    – yshahak
    Sep 21, 2016 at 15:51
  • Well I tried best as per my knowledge, there are some apps available which customize status bar, you should look for them. Maybe u can get lead in proper direction. 😃
    – TapanHP
    Sep 21, 2016 at 17:46
6

I would suggest do not use for usuall Google Play application. There are several reasons:

  1. 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.

    1. 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.

    2. 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.

    3. This is very very ugly UI/UX.

    4. Notifications' workflow is dramatically depend on Android version.

1

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.

2
  • 1
    I have a reason why I don't want the notification on the tray, and basically it's because the small icon meant to stay there 24/7 (like the system battery icon for example).
    – yshahak
    Sep 20, 2016 at 7:22
  • Can you explain how it can be done with InputMethodService.showStatusIcon()? As clearly I have nothing to do at the moment with InputMethodService
    – yshahak
    Sep 20, 2016 at 7:30
0

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.