Android: ProgressDialog doesn't show
Asked Answered
L

4

41

I'm trying to create a ProgressDialog for an Android-App (just a simple one showing the user that stuff is happening, no buttons or anything) but I can't get it right. I've been through forums and tutorials as well as the Sample-Code that comes with the SDK, but to no avail.

This is what I got:

    btnSubmit.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
        (...)
          ProgressDialog pd = new ProgressDialog(MyApp.this);
          pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
          pd.setMessage("Working...");
          pd.setIndeterminate(true);
          pd.setCancelable(false);

          // now fetch the results
          (...long time calculations here...)

          // remove progress dialog
          pd.dismiss();

I've also tried adding pd.show(); and messed around with the parameter in new ProgressDialog resulting in nothing at all (except errors that the chosen parameter won't work), meaning: the ProgressDialog won't ever show up. The app just keeps running as if I never added the dialog.

I don't know if I'm creating the dialog at the right place, I moved it around a bit but that, too, didnt't help. Maybe I'm in the wrong context? The above code is inside private ViewGroup _createInputForm() in MyApp.

Any hint is appreciated,

Lemire answered 9/5, 2010 at 17:7 Comment(0)
A
43

you have to call pd.show before the long calculation starts and then the calculation has to run in a separate thread. A soon as this thread is finished, you have to call pd.dismiss() to close the prgoress dialog.

here you can see an example:

the progressdialog is created and displayed and a thread is called to run a heavy calculation:

@Override
    public void onClick(View v) {
       pd = ProgressDialog.show(lexs, "Search", "Searching...", true, false);
       Search search = new Search(   ...   );
       SearchThread searchThread = new SearchThread(search);
       searchThread.start();
    }

and here the thread:

private class SearchThread extends Thread {

        private Search search;

        public SearchThread(Search search) {
            this.search = search;
        }

        @Override
        public void run() {         
            search.search();
            handler.sendEmptyMessage(0);
        }

        private Handler handler = new Handler() {

            @Override
            public void handleMessage(Message msg) {
                displaySearchResults(search);
                pd.dismiss();
            }
        };
    }
Antediluvian answered 9/5, 2010 at 17:9 Comment(5)
I've rebuild have of my app to get this done :) The tutorials I read sounded like this was possible without another thread, but you never stop learning. I'm not done completely: now the ProgressDialog doesn't show where it should (at least it shows!), but just "flashes" shortly before the results come in, but now I think I can make the rest on my own ;) Thanks a lot for your help!Lemire
Just to complete this, in case somebody is interested: the "flashing" of the ProgressDialog is something that only happens in the emulator, it doesn't occur on the actual device. It seems that the emulator takes loooong seconds before the request (onClick) is actually processed and the app freezes until that happens. On the device, everythings ok.Lemire
Instead of a handler you can also call runOnUiThread which is slightly clearer.Muriah
hey Roflcopter.. my problem with this soultuion you gave is that my thread processing is in a separate class. That is my first class which has all the UI calls the thread class through an object and executes it waiting for some HTTP results ... now My pd.show() will have to be in the class that displays UI but as your solution says.. pd.dismiss() should be in the method that actually does the work, after the work is done... but that is not possible for me.. is any alternate solution possible?? I tried calling pd.dismiss() after the line where the result is received but still no luckGlorify
@thanks RoflcoptrException it is really helpful +1 for you :)Comedown
L
25

I am giving you a solution for it, try this... First define the Progress Dialog in the Activity before onCreate() method

private ProgressDialog progressDialog;

Now in the onCreate method you might have the Any button click on which you will change the Activity on any action. Just set the Progress Bar there.

progressDialog = ProgressDialog.show(FoodDriveModule.this, "", "Loading...");

Now use thread to handle the Progress Bar to Display and hide

new Thread() 
{
    public void run() 
   {
       try
       {
           sleep(1500); 
           // do the background process or any work that takes time to see progress dialog
       }
       catch (Exception e)
       {
           Log.e("tag",e.getMessage());
       }
       // dismiss the progress dialog   
       progressDialog.dismiss();
    }
}.start();

That is all!

Luanneluanni answered 12/4, 2011 at 6:24 Comment(2)
Its showing progressDialog perfectly. But my context is little different. I want to show progressDialog when switching from one application to another and want to avoid black-out. Is it possible?Crack
This is giving me an error Only the original thread that created a view hierarchy can touch its views. See #5162451Cellar
C
4

Progress Dialog doesn't show because you have to use a separated thread. The best practices in Android is to use AsyncTask ( highly recommended ). See also this answer.

Cnidus answered 27/8, 2012 at 7:59 Comment(0)
D
3

This is also possible by using AsyncTask. This class creates a thread for you. You should subclass it and fill in the doInBackground(...) method.

Depress answered 16/8, 2012 at 16:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.