Show ProgressBar in Notification Area like Google does when downloading from Android market

Nico Heid's picture

android download progressWhen you download from the Market you get informed about the progress in the notification area.

That's a really nice feature, which you may want for your own download or processing progress. I will show you the basics of generating such a Notification. All you need to do is add it to your code.

I included a screenshot. You can find the code at github.

First of all, you need your Notification object:

  1.         // configure the notification
  2.         final Notification notification = new Notification(R.drawable.icon, "simulating a download", System
  3.                 .currentTimeMillis());
  4.         notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
  5.         notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.download_progress);
  6.         notification.contentIntent = pendingIntent;
  7.         notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.ic_menu_save);
  8.         notification.contentView.setTextViewText(R.id.status_text, "simulation in progress");
  9.         notification.contentView.setProgressBar(R.id.status_progress, 100, progress, false);

then add it to the NotificationManager

  1.         final NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(
  2.                 getApplicationContext().NOTIFICATION_SERVICE);
  3.         notificationManager.notify(42, notification);

in your Thread call the NotificationManager regularly, after you updated your notification with new values:

  1.         notification.contentView.setProgressBar(R.id.status_progress, 100, progress, false);
  2.         // inform the progress bar of updates in progress
  3.         notificationManager.notify(42, notification);

The XML and the full example, ready to run, can be found on github.

Comments

Kontantinos Polychronis - ProgressBar in Notification Area &'s picture

[...] this tutorial by NICO HEID from [...]

Umar's picture

such a nice tutorial.

do you have any uploading related tutorial?
for example
showing progress while uploading?

Techflag's picture

hi,did you get the demo about "Show ProgressBar in Notification Area like Google does when downloading from Android market ",if you have the demo,would you send one to me!

thanks a lot!

Anonymous's picture

Can you suggest me that how to add the percentage of downloading done in the ProgressBar.... Help will be greatly appreciated.

Nico Heid's picture

just add a text view to the layout https://github.com/nheid/unitedcoders-android/blob/master/res/layout/dow... and write the progress into it.

Sundar's picture

I want to show the progress bar in notification area while uploading the video and i want to remove the progress bar after the video has been uploaded.could u help me in such a way?

Alik's picture

Great example.
The USB notification text and icon are much more darker.
How can I set the same look and feel?
Thanks.

butelo's picture

Very nice tx
How can I make the progress bar dissapear when the task is done?

nico's picture

you should find your answer here: http://developer.android.com/reference/android/app/NotificationManager.html#cancel(int