Skip to content

SimpleProgressDialog

Eltos edited this page Oct 29, 2021 · 6 revisions

Progress dialog

extends CustomViewDialog

API reference Examples

A dialog showing a progress

Usage

For general usage see SimpleDialog.

The following build methods are provided for convenience (see examples below for screenshots)

  • SimpleProgressDialog.build() - default
  • SimpleProgressDialog.buildBar() - horizontal progress bar with percentage indicator
  • SimpleProgressDialog.buildIndeterminateCircle() - spinning circle for indeterminate progress

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) and cancellation can be enabled via cancelable(true).

Additional methods are provided to set the progress type (type), percentage visibility (percentage) etc.

Please refer to the API reference for a comprehensive documentation of these methods.

Updating progress

The progress is indeterminate by default, but can be updated either before the dialog is shown or while it is being shown using the update methods (updateProgress, updateSecondaryProgress, updateMax, updateIndeterminate, updateFinished, updateProgressText, updateInfoText), please refer to the API reference for a comprehensive documentation of these methods.

// Get the dialog by its tag and update the progress
FragmentManager fm = getSupportFragmentManager();
SimpleProgressDialog dialog = (SimpleProgressDialog) fm.findFragmentByTag(PROGRESS_DIALOG_TAG);

dialog.updateProgress(13, 100);
dialog.updateProgressText("5 MB"); // for a short text (by default, this shows the progress in percent unless manually set)
dialog.updateInfoText("Working…"); // for a longer text
dialog.dismiss();

Working with AsyncTask

An AsyncTask can easily be connected to the dialog, such that the progress and state is automatically synced with the task.

Create a task extending SimpleProgressTask:

static class MyProgressTask extends SimpleProgressTask<Void, Integer, Void> {
    @Override
    protected Void doInBackground(Void... voids) {
        // Use these methods to update the progress:
        publishProgress(-1); // Indeterminate
        publishProgress(10); // 10% progress
        publishProgress(100, 500);  // 100/500 = 20% progress
        publishProgress(20, 100, 40);  // 20% primary and 40% secondary progress
        // ... you can also set text via publishProgess(...), see javadoc for details
    }
    // if required, the full spectra of `mDialog.updateXXX(...)` methods can be called from a custom `onProgressUpdate` implementation
}

Then pass the task to the task method:

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

boolean cancelable = true;
boolean autoDismiss = false;

SimpleProgressDialog.buildBar()
                    .title(R.string.login)
                    .msg(R.string.creating_user_profile_wait)
                    .task(task, cancelable, autoDismiss)  // <-- your task
                    .show(this, PROGRESS_DIALOG);

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

For general usage see SimpleDialog.

Note the special value SimpleProgressDialog.COMPLETED for the which parameter in onResult for completed and auto-dismissed AsyncTasks.

Examples

SimpleProgressDialog.build()
                .title(R.string.login)
                .msg(R.string.creating_user_profile_wait)
                .show(this, PROGRESS_DIALOG);
static class MyProgressTask extends 
        SimpleProgressTask<Void, Integer, Void> {
    @Override
    protected Void doInBackground(Void... voids) {
        SystemClock.sleep(500);
        for (int i = 0; !isCancelled() && i < 100; i+=1) {
            publishProgress(i+25, 150);
            SystemClock.sleep(10);
        }
        return null;
    }
}
    
MyProgressTask task = new MyProgressTask();
task.execute();

SimpleProgressDialog.buildBar()
        .title(R.string.login)
        .msg(R.string.creating_user_profile_wait)
        .task(task, true, false)
        .show(this, PROGRESS_DIALOG);
static class MyProgressStringTask extends
        SimpleProgressTask<Void, Pair<Integer, String>, Void> {
    @Override
    protected Void doInBackground(Void... voids) {
        publishProgress(new Pair<>(-1, "Starting…"));
        SystemClock.sleep(1000);
        for (int i = 0; !isCancelled() && i < 100; i+=1) {
            publishProgress(new Pair<>(i, i < 40 ? "Working…"
                                        : i < 85 ? "Still working…" 
                                        : "Almost done…"));
            SystemClock.sleep(10);
        }
        publishProgress(new Pair<>(100, "Finished"));
        return null;
    }
}
    
MyProgressStringTask task = new MyProgressStringTask();
task.execute();

SimpleProgressDialog.buildIndeterminateCircle()
        .title(R.string.login)
        .msg(R.string.creating_user_profile_wait)
        .task(task, false, true)
        .show(this, PROGRESS_DIALOG);
Clone this wiki locally