ProgressDialog does not want to update the message
Asked Answered
O

2

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.

Oystercatcher answered 16/10, 2010 at 0:28 Comment(0)
O
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);
    }
};
Oystercatcher answered 16/10, 2010 at 1:54 Comment(8)
this does not work. i try to update the message from the UI Thread but it does not change.Bernicebernie
Yeah, this is so easy to help with "this is not working" as message :-pOystercatcher
@Oystercatcher 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?Argonaut
This is for you: #6450775Oystercatcher
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.Robinet
@JMLord Thanks, I think you should add it as an answer, it solved my problem!Affranchise
@JMLord Yeah, thanks for pointing that out. Another annoying Android API bug that will probably never get fixed.Coral
@ JM Lord Thanks it was the issue.Elea
G
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();
}
Goldston answered 3/2, 2021 at 11:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.