Skip to content
Search

Android.app.ProgressDialog

lastmod 2018-08-17created 2016-06-301 edits

A dialog showing a progress indicator and an optional text message or view. Only a text message or a view can be used at the same time. The dialog can be made cancelable on back key press.

The progress range is 0..10000.

Example of handling Cancel on Android

The code below is an example that handles cancellation in an AsyncTask when the ProgressDialog's cancel button is clicked.

class MyTask extends AsyncTask<Void, Void, Void> {
    private volatile boolean running = true;
    private final ProgressDialog progressDialog = null;

    public MyTask(Context ctx) {
        // progressDialog = gimmeOne(ctx);

        progressDialog.setCancelable(true);
        progressDialog.setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                // actually could set running = false; right here, but I'll
                // stick to contract.
                cancel(true);
            }
        });
    }

    @Override
    protected void onPreExecute() {
        progressDialog.show();
    }

    @Override
    protected void onCancelled() {
        running = false;
    }

    @Override
    protected Void doInBackground(Void... params) {
        while (running) {
            // does the hard work
        }
        return null;
    }
    // ...
}

See also

Favorite site

References


  1. Android_download_progress.pdf ↩