How To Show A Progress Dialog in Android


When you need to do some long running task and show a “wait” dialog to the user while the task is running, a Progress Dialog is the best way to go. When you do not know how long the task will take , then you show a Spinner instead of a horizontal progress bar. Here we will use the example of using a Spinner while authenticating a user after he has filled in a login form.



In our layout xml we called the doLogin() function when the Login button is clicked. This function will in turn execute an AsyncTask which will execute a backend process. The ProgressDialog will be shown before the AsyncTask starts execution and will go away once the AsyncTask has finished execution.

public class LoginActivity extends ActionBarActivity {

    ProgressDialog mProgressBar;

    protected void onCreate(Bundle savedInstanceState) {

        mProgressBar = new ProgressDialog(this);

 public void doLogin(View view) {
        mProgressBar.setMessage("Authenticating your login..");
        new LoginTask().execute(0);



    // The params are dummy and not used
    class LoginTask extends AsyncTask<Integer, Integer, String> {
        protected String doInBackground(Integer... params) {

                try {
                    // some long running task will run here. We are using sleep as a dummy to delay execution

                } catch (InterruptedException e) {

            return "Task Completed.";
        protected void onPostExecute(String result) {
            // do whatever needs to be done next
        protected void onPreExecute() {
        protected void onProgressUpdate(Integer... values) {



  1. @geethadevi,

    AsyncTask is a class which is used to execute long running processes. Mostly its used when data has to be sent or received from a URL or from a database. Android does not allow long running taskings to be called on the main thread as it will block the system event loop and the device will freeze. Calling AsyncTask does not block the main thread as the process runs on a background thread.
    You can find more details here:

Leave a Reply

Your email address will not be published.