36

I just tried to implement a progressdialog and I have some issues to change the text during my long and complex calculations.

for (String aString:myStringArray){
    Log.v(TAG, aString);
    mProgressDialog.incrementProgressBy(1);
    mProgressDialog.setMessage(aString);
}

I can clearly see the incrementProgressBy working and my dialog updating, but the message does not change.

Any idea on how to make that work?

Thank a lot.

Justin Johnson
  • 30,978
  • 7
  • 65
  • 89
Waza_Be
  • 39,407
  • 49
  • 186
  • 260

2 Answers2

60

Just found the answer, that's working fine:

runOnUiThread(changeMessage);

with that code:

private Runnable changeMessage = new Runnable() {
    @Override
    public void run() {
        //Log.v(TAG, strCharacters);
        m_ProgressDialog.setMessage(strCharacters);
    }
};
Waza_Be
  • 39,407
  • 49
  • 186
  • 260
  • 2
    this does not work. i try to update the message from the UI Thread but it does not change. – dy_ Apr 01 '14 at 18:04
  • 5
    Yeah, this is so easy to help with "this is not working" as message :-p – Waza_Be Oct 13 '14 at 07:23
  • 1
    @Waza_Be Sorry for being so direct :D Actually I tried your code to update the message of `ProgressDialog` but the `ProgressDialog` just hangs with older message in it. I am trying to update the message from `doInBackground()` of `AsyncTask`. Any idea how to do it? – Amit Anand Oct 16 '14 at 07:41
  • 1
    This is for you: http://stackoverflow.com/questions/6450275/android-how-to-work-with-asynctasks-progressdialog – Waza_Be Oct 16 '14 at 07:44
  • 5
    Comment on your original answer : this was exactly what I was doing while having the refresh problem. In my case, it was because a message was not set before showing the dialog, and could not be added after. By putting an initial message with the same code, updating it later on it works fine. – JM Lord Nov 25 '15 at 19:47
  • @JMLord Thanks, I think you should add it as an answer, it solved my problem! – B-GangsteR Feb 23 '17 at 07:11
  • @JMLord Yeah, thanks for pointing that out. Another annoying Android API bug that will probably never get fixed. – m0skit0 Jun 06 '17 at 15:17
  • @ JM Lord Thanks it was the issue. – alierdogan7 Sep 20 '18 at 08:19
0

I upload pictures to Firebase in a loop and updating the ProgressDialog each image:

(I am in a Fragment, so I use getActivity() before calling to runOnUiThread())

List<Bitmap> bitmaps;
int picCounter = 1;
...

progressDialog = ProgressDialog.show
 (getContext(), "sending...", "just a minute", false, false);

new Thread(new Runnable() {
    @Override
    public void run() {
        for (int i = 0; i < bitmaps.size(); i++) {

            String filename = String.valueOf(i);

            uploadPic(bitmaps.get(i), "img" + filename, new MyCallback() {
                @Override
                public void onFinish() {
                    picCounter++;
                    Objects.requireNonNull(getActivity()).runOnUiThread(new Runnable() {
                        public void run() {
                            progressDialog.setTitle ("upoading " + picCounter + "image from " + bitmaps.size());
                        }
                    });
                }
            });
        }
   }
}).start();

uploadPic method:

public interface MyCallback { void onFinish ();}

private void uploadPic(final Bitmap bitmap, final String fileName, final MyCallback callback) {
   ... // uploading to firebase and then:
   callback.onFinish();
}
Avital
  • 549
  • 5
  • 15