8

There are several threads already on how to make custom layouts in the notification bar. The problem is I must be missing something simple.

I have a custom_notification_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="3dip"
              >

    <TextView android:id="@+id/text"
              android:text="Uploading"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textColor="#000"
              />

    <ProgressBar  
            style="?android:attr/progressBarStyleHorizontal" 
            android:layout_height="wrap_content" 
            android:layout_width="fill_parent"
            android:max="0" 
            android:progress="0"   
            android:layout_marginLeft="10dip"  
            android:id="@+id/progressBar" 
            />  
</LinearLayout>

I also have some test code that creates the notification, which works and shows the progress bar.

NotificationManager mManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon, title, System.currentTimeMillis());
RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification_layout);
contentView.setProgressBar(R.id.progressBar, 10, 0, false);        
contentView.setTextViewText(R.id.text, text);       
notification.contentView = contentView;

Intent notificationIntent = new Intent(context, NotificationHandler.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
mManager.notify(APPID, notification);

Finally I try to update the progress bar, which doesn't work.

contentView.setProgressBar(R.id.progressBar, 10, 5, false); 

What is the secret to actually updating the notification?

Cameron McBride
  • 6,779
  • 11
  • 40
  • 52

4 Answers4

11

You should add these two lines:

// update the notification object
notification.contentView.setProgressBar(R.id.progressBar, 10, 5, false);
// notify the notification manager on the update.
mManager.notify(APPID, notification);
Justin
  • 84,773
  • 49
  • 224
  • 367
Erez
  • 156
  • 2
  • 4
  • 1
    The problem with this is, that it actually makes the notification jump around (position wise) in the notification area, if there are e.g. other downloads running which also constantly refresh their progress bars... However I don't know if you can do something about this... – ubuntudroid May 31 '11 at 15:19
  • 2
    You probably forgot to add `FLAG_ONGOING_EVENT` to your notification. – Xion Aug 21 '11 at 11:45
  • FLAG_ONGOING_EVENT does NOT help. The notification keeps jumping. Tested on an Asus transformer tablet. – AlikElzin-kilaka Dec 05 '11 at 17:47
  • 4
    For the jumping problem, use the flag `Notification.FLAG_ONLY_ALERT_ONCE` – AlikElzin-kilaka Dec 06 '11 at 19:42
  • Doesn't work for me - tried FLAG_ONGOING_EVENT | FLAG_ONLY_ALERT_ONCE also. I found that the Asus Transformer TF101, when plugged to dock, shows "Docking connected", when plugged to PC shows "ASUS Sync", and as long as either of these notifications are on, it will cause the jumpiness. The jumpiness also happens with the Google Play Shop, when you download an app. – albnok Apr 05 '12 at 04:28
6

Remember not to notify the status bar too often. If you use that code inside, for example, a onProgressUpdate of a AsyncTask and you notify EVERY progress, you will virtually block the status bar. Notify only when there are changes.

DeliriumTremens
  • 340
  • 5
  • 14
1

I had issues while updating the progress bar too often (I'm using a NotificationCompat.Builder to do the job) which was causing the notification area to block. I solved the issue by skipping updates if they occurs within a minimum interval time like this:

private static final long MIN_UPDATE_INTERVAL = 10000000;

private long lastUpdateTime = System.nanoTime();

and in my update callback:

// Don't update too often
if ((System.nanoTime() - lastUpdateTime)< MIN_UPDATE_INTERVAL) return;

builder.setProgress(max, currentValue, false);
notificationManager.notify(notificationID, builder.build());

lastUpdateTime = System.nanoTime();

This prevent bloking the notiicaton area and also allows a smooth update of the progress bar

lviggiani
  • 5,824
  • 12
  • 56
  • 89
1

In your layout file, you have the progressbars max set at 0. If it maxes at 0, it cannot go higher than 0. Set it to 100

jb15613
  • 359
  • 4
  • 12