Problem using ProgressDialog with onCreateDialog / onPrepareDialog
Asked Answered
M

1

4

I'm using the following code to create a ProgressDialog (inside my Activity):

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case DIALOG_LOOKUP:
            return new ProgressDialog(this, ProgressDialog.STYLE_SPINNER);
    }
    return null;
}

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    switch (id) {
        case DIALOG_LOOKUP:
            dialog.setCancelable(true);
            dialog.setTitle(R.string.dialogLookup_title);
            ((ProgressDialog)dialog).setMessage(getResources().getString(R.string.dialogLookup_message));
            dialog.setOnCancelListener(new OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    Toast.makeText(MyActivity.this, "canceled", Toast.LENGTH_SHORT).show();
                }
            });
            break;
    }
}

The problem is that it isn't actually setting the title and is putting it in some weird double-box.

It's giving me this:

bad progress

but I'm expecting something more like this:

good progress

Any ideas?

Mauldon answered 3/2, 2010 at 21:13 Comment(2)
Is your onPrepareDialog a method on a subclass of Dialog?Effuse
No. As I said, they're in the Activity: developer.android.com/reference/android/app/…Mauldon
A
6

I just tried your sample and it seems changing from ProgressDialog.STYLE_SPINNER to ProgressDialog.STYLE_HORIZONTAL fixed the weird double-box problem.

And it also displays the title and text.

Edit:

You are passing ProgressDialog.STYLE_SPINNER in the ProgressDialog constructor.

From the doc, the 2nd argument is a theme id.

You will have to create a ProgressDialog object and use the setProgressStyle to ProgressDialog.STYLE_SPINNER

case DIALOG_LOOKUP:
     ProgressDialog pdlg = new ProgressDialog(this);
     pdlg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
     return pdlg;
Awash answered 4/2, 2010 at 0:20 Comment(3)
D'oh... how could I have missed that? I'm still not getting a title though.Mauldon
Well, for some reason I had to move my setTitle() into onCreateDialog(), but that seems to have done it. Thanks for the help.Mauldon
It was a long time ago, but in case it helps anyone: I found that that dialog.setTitle(...) only works in the onPrepareDialog() if it is also set in the onCreateDialog(). (tried it on 4.1.2)Arianna

© 2022 - 2024 — McMap. All rights reserved.