0

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.

aagarwal
  • 134
  • 1
  • 2
  • 10

3 Answers3

3

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

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • I have an Async task in the program that I am using and it has one protected Boolean doInBackground(Integer...params) method. I have tried doing the above inside the doInBackground function but the same error occurred. – aagarwal Apr 17 '13 at 19:03
  • I tried integrating what was in that post into my code before I posted the question. But it didn't work. So I posted the question myself. – aagarwal Apr 17 '13 at 19:18
  • @aagarwal `onPostExecute()` gets called after `doInBackground()` finishes so you need to do some work in there like your `Thread.sleep()` call – codeMagic Apr 17 '13 at 19:21
  • @codeMagic As he isn't got the solution, I have undelete my answer and posted the one.. – Pragnani Apr 17 '13 at 19:27
  • @Pragnani no problem. Its good for everyone to see different options – codeMagic Apr 17 '13 at 19:30
1

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);
Pragnani
  • 20,075
  • 6
  • 49
  • 74
  • I missed the super.onCreate(savedInstanceState); in the question, it was always there in the original code. I added this line finally { pg.dismiss(); } after the try/catch statement. But the problem persists. – aagarwal Apr 17 '13 at 19:07
0

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();
Ahsan
  • 29
  • 1
  • Code only answers are generally frowned upon in SO :( Its good for members to know WHY the code will work so they can help themselves later on – codeMagic Apr 17 '13 at 19:36
  • This works perfectly however its the same as the code in codeMagic's example so I gave the check to him. – aagarwal Apr 17 '13 at 19:49