aagarwal
aagarwal

Reputation: 134

Unable to close Progress Dialog in Android

I want to create a progress dialog in android, keep it open for 2 seconds then close it.

Below is the code I have written:

package com.example.proressdialogtest;

import android.app.activity;
import android.app.ProgressDialog;
import android.os.bundle;

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState); 
        ProgressDialog pg = new ProgressDialog(MainActivity.this, 4);

        pg.show(MainActivity.this, null, "Searching...", false);

        try {
            Thread.sleep(2000);
        } catch (Exception e) {
            e.printStackTrace();
        }  

        pg.dismiss();
}

When I run the code on my device, the ProgressDialog is opened and then it stays open, it does not close after 2 seconds. What am I doing wrong?


As per the answers below. I have added the onPreExecute() and onPostExecute() methods before and after the doInBackground method respectively.

Here is the code for the two methods.

ProgressDialog pd;

public void onPreExceute() {
    pd = new ProgressDialog(MainActivity.this);
    pd.show(MainActivity.this, "", "Searching...", false);
}

public void onPostExecute() {
    pd.dismiss();
}

The problem still persists. The progress bar will not close.

Upvotes: 0

Views: 3708

Answers (3)

Pragnani
Pragnani

Reputation: 20155

Edit:

Use this code

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        final ProgressDialog pg ;

      pg =  ProgressDialog.show(MainActivity.this, null, "Searching...", false);

        Thread timer=new Thread(){
            public void run()
            {
                try {
                    sleep(2000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                finally{
                     pg.dismiss();
                }
            }

        };
        timer.start();
}
}

You are pausing the UI thread by calling

 Thread.sleep(2000);

Because Thread.sleep(x) will make the current thread sleep for x milli seconds

which is bad thing.

place

I don't know why

pg.dismiss() in finally block to make sure that progress dialog will be closed.

And also your Code won't run because you have missed

super.onCreate(savedInstanceState);

Upvotes: 1

Ahsan
Ahsan

Reputation: 29

following code will work for you,

 new AsyncTask<Integer, Integer, Boolean>()
             {
                 ProgressDialog progressDialog = null;

                 @Override
                 protected void onPreExecute()
                 {                         
                     progressDialog = ProgressDialog.show(MainActivity.this, "",
                             "Loading...");
                 }

                 @Override
                 protected Boolean doInBackground(Integer... params)
                 {
                     if (params == null)
                     {
                         return false;
                     }
                     try
                     {

                         Thread.sleep(2000);

                     }
                     catch (Exception e)
                     {

                         return false;
                     }

                     return true;
                 }

                 @Override
                 protected void onPostExecute(Boolean result)
                 {
                     progressDialog.dismiss();                        

                 }
             }.execute();

Upvotes: 0

codeMagic
codeMagic

Reputation: 44571

You are calling sleep() on the UI Thread. Don't do this. Use runOnUiThread or an AsyncTask

Painless Threading

AsyncTask

I would use an AsyncTask. You will start the ProgressDialog in onPreExecute() then close it in onPostExecute(). It is a UI element so using it in doInBackground() will give you the error

This SO answer has a good example of using it

Upvotes: 3

Related Questions