Skip to content

SimpleProgressDialog

Philipp Niedermayer edited this page May 28, 2021 · 6 revisions

Progress dialog

extends CustomViewDialog, Full API reference, added in v3.4

A dialog showing a progress

Building dialogs

The progress is indeterminate by default, but can be updated either before the dialog is shown or while it is being shown. In addition, an AsyncTask can easily be connected to the dialog, such that the progress and state is automatically synced with the task.
By default, a cancel button is shown, but the dialog can not be dismissed by (accidentally) clicking outside it or via the back button. This button can be removed via .neut(null).
The dialog is meant to be dismissed by calling .dismiss(), but an auto-dismiss option exists.

Example

SimpleProgressDialog.bar()
                    .title(R.string.login)
                    .msg(R.string.creating_user_profile_wait)
                    .show(this, PROGRESS_DIALOG);
// use this to access the dialog after rotation changes
FragmentManager fm = getSupportFragmentManager();
SimpleProgressDialog dialog = (SimpleProgressDialog) 
    fm.findFragmentByTag(PROGRESS_DIALOG);

dialog.updateProgress(13, 100);
dialog.updateInfoText("Working…");
dialog.dismiss();

More examples can be found in the testApp

Available customizations

  • Type (bar/circle)
    SimpleProgressDialog.bar()
    SimpleProgressDialog.indeterminateCircle()
    .type(Type type)
    Please note that the circle type can only show an indeterminate progress

  • Percentage indicator "00%" below bar/inside circle
    (shown by default for bar type)
    .percentage(boolean visible)

See also CustomViewDialog

Updating the progress

These methods can be called either before the dialog is shown or afterwards.

Progress
.updateProgress(int progress)
.updateProgress(int progress, int max)
.updateMax(int max)
.updateSecondaryProgress(int progress)
.updateIndeterminate()
.updateFinished()

Text
.updateProgressText(String text) for a short text, e.g. 5% or 8MB (by default, this shows the progress unless manually set)
.updateInfoText(String text) for a longer text, e.g. "Loading data…" or "5 seconds left"

Working with AsyncTask

Derive a task from SimpleProgressTask.

static class MyProgressTask extends SimpleProgressTask<Void, Integer, Void> {
    @Override
    protected Void doInBackground(Void... voids) {
        publishProgress(10); // 10% progress
        publishProgress(100, 500);  // 100/500 = 20% progress
        publishProgress(20, 100, 40);  // 20% primary and 40% secondary progress
        // ...
    }
    // if required, the full spectra of `mDialog.updateXXX(...)` methods can be called from a custom `onProgressUpdate` implementation
}

and link it to the dialog:

MyTask task = new MyTask();
task.execute();

SimpleProgressDialog.bar()
        .title("...")
        .msg("...")
        .task(task, cancelable, autoDismiss) // optional cancel button and auto-dismiss functionality
        .show(this);

If cancelable is set and the cancel button is pressed, the task is canceled with mayInterruptIfRunning=false, so make sure to frequently check for ìsCancelled() in your tasks doInBackground method.
If autoDismiss is set, the dialog will be dismissed once the task is finished and the onResult method will be called with which=SimpleProgressDialog.COMPLETED.

Receiving results

See SimpleDialog

Clone this wiki locally