Navdroid
Navdroid

Reputation: 4533

Using progressDialog in android?

I am using this code to display a Progress Dialog which is working fine:

 dialog = ProgressDialog.show(this, "Please wait", 
 "Gathering Information...", true);
   Thread thread = new Thread()
    {
     @Override
        public void run() {
        if(Chapter_sync.size()>0){
        storemodule();

         c.open();
         for(int i=0;i<Chapter_sync.size();i++)
           {
             downloadPDF(Chapter_sync.get(i));
             System.out.println("SYNCED"+i);
             c.update(Chapter_sync.get(i));
           }
           }dialog.dismiss();                           
          }
       };thread.start();

         LinearLayout parentlayout=(LinearLayout)findViewById(R.id.chapterholder);
         parentlayout.removeAllViews();

         setUpViews();

       }
   }

Here what I am trying to do is display a Progress dialog till all computation is done. As it completes i wanted to setup all views again. But the setUpViews() is called before the thread starts. I am not so good at thread basics .Could any one help me understand why is this happening and how can I get my own results?

Upvotes: 0

Views: 922

Answers (3)

Vivek Khandelwal
Vivek Khandelwal

Reputation: 7849

In your code if u see

After Starting the Thread you have call your method setUpViews(), which does not wait for your thread to complete and setups your views.

Use Handler.post after the dialog is dismissed in your thread which gather your information.

handler.post(new Runnable()
{
setUpViews();
});

So after the your operations completed your setupViews will be called by your Handler.

Upvotes: 0

Andro Selva
Andro Selva

Reputation: 54332

The problem is you are not using handlers. Simply do this,

dialog = ProgressDialog.show(this, "Please wait", 
 "Gathering Information...", true);
   Thread thread = new Thread()
    {
     @Override
        public void run() {
        if(Chapter_sync.size()>0){
        storemodule();

         c.open();
         for(int i=0;i<Chapter_sync.size();i++)
           {
             downloadPDF(Chapter_sync.get(i));
             System.out.println("SYNCED"+i);
             c.update(Chapter_sync.get(i));
           }
           }dialog.dismiss();                           
          }
         handler.sendemptyMessage(0);
       };thread.start();

And in your onCreate() create Handlers,

Handler handler=null;
handler=new Handler()
{
 public void handleMessage(Message msg)
{
 progressDialog.cancel();
  if(msg.what==0)
{
LinearLayout parentlayout=(LinearLayout)findViewById(R.id.chapterholder);
         parentlayout.removeAllViews();

         setUpViews();
};

You can't update your UI from background thread. Either you have to use AsyncTask or to use handlers from your background thread to inform your main thread that the background action has been completed.

Upvotes: 2

soumyajit das
soumyajit das

Reputation: 131

Thread scheduling is dependent on the operating system. So instantiating your thread does not ensure that your thread will run whenever you want.

The problem you are facing can be best handled using async task. Or if you have a callback that lets you know when your download is completed then you can dismiss the dialog on the callback. Make sure you dismiss it inside a UI thread by doing.

mActivity.runOnUiThread() or any other such methods.

Upvotes: 0

Related Questions