Refresh progress bar in notification bar
Asked Answered
K

3

20

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.

Knothole answered 22/4, 2010 at 9:49 Comment(3)
I don't know if it is possible. Have you tried with an animated gif?Gers
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.Knothole
im sure its possible. Market app shows a progress bar when downloading and installing apps in the notification bar...Gemot
G
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();
    }
}
Gemot answered 27/4, 2010 at 21:19 Comment(3)
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 developer.android.com/reference/android/widget/…, int, int, boolean) they say to give "The id of the view whose text should change."Bluster
@Bluster - 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.Warmongering
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 progressbarBenavides
M
1

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

Meantime answered 27/4, 2010 at 20:58 Comment(1)
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.Piracy
L
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.

Lacustrine answered 25/12, 2013 at 11:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.