Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating the Progress Bar in the Notification Area

Tags:

android

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?

like image 617
Cameron McBride Avatar asked Aug 21 '10 13:08

Cameron McBride


People also ask

How do I show progress bar in notification?

To display a determinate progress bar, add the bar to your notification by calling setProgress(max, progress, false) and then issue the notification. The third argument is a boolean that indicates whether the progress bar is indeterminate (true) or determinate (false).

How can we display progress bar in android?

In android there is a class called ProgressDialog that allows you to create progress bar. In order to do this, you need to instantiate an object of this class. Its syntax is. ProgressDialog progress = new ProgressDialog(this);

What is progress notification?

In android, Progress Notification is used to show the progress of an ongoing operation in the notification bar. By using progress notification, we can easily know how much percentage of the current operation completed and how long the operation will run to complete the remaining operation.

How do I make my progress bar horizontal android?

In Android, by default a progress bar will be displayed as a spinning wheel but If we want it to be displayed as a horizontal bar then we need to use style attribute as horizontal. It mainly use the “android. widget. ProgressBar” class.


3 Answers

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);
like image 160
Erez Avatar answered Sep 22 '22 03:09

Erez


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.

like image 23
DeliriumTremens Avatar answered Sep 19 '22 03:09

DeliriumTremens


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

like image 37
jb15613 Avatar answered Sep 21 '22 03:09

jb15613