19

I would like to put a progress bar in the notification bar. The idea is showing the progress bar while the program uploads a file to a server. Everything else is ok, but I can not figure out how to refresh the progress bar inside the notification. Does anybody knows any pattern to play with? I mean, where I should refresh the progress bar, in a service or activity and so.

Patrick McElhaney
  • 52,844
  • 37
  • 123
  • 157
ahmontero
  • 343
  • 1
  • 6
  • 12
  • I don't know if it is possible. Have you tried with an animated gif? – Macarse Apr 22 '10 at 12:41
  • Not yet, but I need to show in real time the upload progress, so somehow I need to update the progress bar. I think that using an animated gif is valid if you only show a "loading" message or so. Anyway thanks for your time. – ahmontero Apr 22 '10 at 13:42
  • im sure its possible. Market app shows a progress bar when downloading and installing apps in the notification bar... – seanmonstar Apr 27 '10 at 20:59

3 Answers3

17

I don't know what your code looks like, so I don't know what you need to modify, butI did some searching through the documentation. I found some stuff on Notifications, ProgressBars, and RemoteViews.

Specifically, in RemoveView, you can update the Progress bar. So combining some of the example code in each link, I get something like this:

public class MyActivity extends Activity {
    private static final int PROGRESS = 0x1;
    private static final int MAX_PROGRESS = 100;

    private int mProgressStatus = 0;

    private Handler mHandler = new Handler();

    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        //define Notification
        //...

        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
        contentView.setProgressBar(R.id.progress_bar, MAX_PROGRESS, mProgressStatus, false);
        notification.contentView = contentView;

        // Start file upload in a background thread
        new Thread(new Runnable() {
            public void run() {
                while (mProgressStatus < MAX_PROGRESS) {
                    mProgressStatus = doWork();

                    // Update the progress bar
                    mHandler.post(new Runnable() {
                        public void run() {
                            contentView.setProgressBar(R.id.progress_bar, MAX_PROGRESS, mProgressStatus, false);
                        }
                    });
                }
            }
        }).start();
    }
}
seanmonstar
  • 10,888
  • 2
  • 19
  • 25
  • 2
    do you have to have a progressbar defined in your custom xml layout? Or will RemoteViews.setProgressBar() take care of that. The reason I ask is because http://developer.android.com/reference/android/widget/RemoteViews.html#setProgressBar(int, int, int, boolean) they say to give "The id of the view whose text should change." – Pzanno Feb 16 '11 at 02:07
  • @Pzanno - I wondered the exact same thing (thus the +1). I have found that you do have to define your own progress bar in your custom xml layout. – ArtOfWarfare Dec 07 '12 at 16:54
  • great example...i am also using progressbar in my custome view but...it hangs my system i dont know why...everything goes smooth with default notification progressbar – H Raval Aug 05 '16 at 09:34
1

You can use custom views in Notification:
https://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomExpandedView

Vadim Kotov
  • 7,103
  • 8
  • 44
  • 57
Rorist
  • 770
  • 7
  • 9
  • It turns out this answer is the only one which worked for me, but there is such a tremendous amount of information to read that you couldn't really put it all in a concise answer. The link is to official documentation, if it changes just update the link. – intrepidis May 31 '15 at 22:18
0

To remove a ProgressBar from RemoteView use the following code :-

 remoteViews.setViewVisibility(R.id.progressBar, View.INVISIBLE);

You can also use View.GONE but that will make android to fill empty space.

Rohan Kandwal
  • 8,402
  • 7
  • 67
  • 103