ProgressDialog not showing up in activity

16,957

I think your code is wrong in a sense that you do all in the UI thread. You have to put callsomefunction() into a background thread.

public void runSomething()
{
    showDialog(BACKGROUND_ID);
    Thread t = new Thread(new Runnable() 
    {                   
        public void run() 
        {
            //do something
            handler.post(finishThread);
        }
    });

    t.start();
    // The progress wheel will only show up once all code coming here has been executed
}

And as well

protected Dialog onCreateDialog(int id)
{
    if(progressDialog == null) progressDialog = new ProgressDialog(this);
    return progressDialog;
}

@Override
protected void onPrepareDialog(int id, Dialog dialog)
{
    if(id == BACKGROUND_ID) 
    {
        progressDialog.setIndeterminate(true);
        progressDialog.setCancelable(false);
        progressDialog.setMessage("running for long ...");
    }
}

Runnable finishThread = new Runnable()
{       
    public void run() 
    {
//long running
        if(progressDialog != null) progressDialog.dismiss();
    }
};
Share:
16,957
webgenius
Author by

webgenius

Updated on June 17, 2022

Comments

  • webgenius
    webgenius about 2 years

    I am trying to include a ProgressDialog in my application. But it is not showing up.

    Here's the code snippet where i use the ProgressDialog:

    public class abcActivity extends Activity {
        public boolean onOptionsItemSelected(MenuItem item) {
            case XYZ:
                ProgressDialog dialog = ProgressDialog.show(abcActivity.this, "", "Please wait for few seconds...", true);
                callSomeFunction();
                dialog.dismiss();
                showToast(getString(R.string.SomeString));
                break;
        }
    }
    

    Does anyone know why the dialog is not showing up? Any clues?

  • xil3
    xil3 over 13 years
    That's a good point, but that's not the problem he's asking about.
  • xil3
    xil3 over 13 years
    This doesn't really help answer the question - we've established that he knows how to use ProgressDialog, and the way he's done it is correct. Actually, the way he has it is way neater than what you've just done. The problem lies elsewhere. And why do you have a method that returns Dialog if you've made dialog1 a public property? Doesn't make much sense to me...
  • dave.c
    dave.c over 13 years
    +1 It's not his exact point, but @Vladimir Ivanov provided a better way of performing a long running task. In fact, since callSomeFunction() is running on the UI thread, could it be blocking the creation of the dialog?
  • webgenius
    webgenius over 13 years
    Vladimir: The link provided by you is broken. Kindly re-check.
  • Vladimir Ivanov
    Vladimir Ivanov over 13 years
    @webgenius Thanks a lot, fixed.
  • dave.c
    dave.c over 13 years
    @xil3 onCreateDialog(int id) is an overridden method on the Activity class, it has to return Dialog.
  • webgenius
    webgenius over 13 years
    Yes true. When I moved the ProgressDialog to a new thread, it started to work. Thanks for the help.
  • Michael Krauklis
    Michael Krauklis about 13 years
    It's not ProgressDialog. It's any UI component. You're in the UI thread, so you cannot update other UI components while you are using the thread to do something else. Either spawning another thread to update the UI, or spawning another thread to perform your work are acceptable solutions.