0

I am working on a program that searches the users phone for some date, which takes about 2-3 seconds. While it's computing I want to display a loading screen, so the user knows something indeed is happening. However, when I try to display a loading screen before the computations, nothing is displayed on the screen.

This is what I have:

        ProgressDialog loading= new ProgressDialog(this);
        loading.setTitle("Loading");
        loading.setMessage("Please wait...");
        loading.show();

       //search stuff
        loading.dismiss();

In addition to this, I have tried putting the ProgressDialog in a thread like the following,

new Thread(new Runnable(){
            public void run(){
              ProgressDialog loading= new ProgressDialog(this);//error here for "this"
              loading.setTitle("Loading");
              loading.setMessage("Please wait...");
              loading.show();
            }
        });
//search stuff

but it fails due to the "this" keyword, I believe because its referring to an Activity and not a regular class, but I could be wrong...

How can I get the ProgressDialog to display properly?

swag antiswag
  • 349
  • 1
  • 4
  • 12
  • Why are you creating a new Thread to actually show the loading bar rather than creating a new thread to do the heavy processing that you talked about... and use the UI thread (ur current thread ) to show the progress bar etc... – N Jay Nov 02 '15 at 04:09
  • If you are doing the searching part in a different thread then call `loading.dismiss()` in the end in that thread itself. – camelCaseCoder Nov 02 '15 at 04:54

3 Answers3

1

Try to handle it in this way

mProgressDialog = ProgressDialog.show(this, "Please wait","Long operation starts...", true);
        new Thread() {
            @Override
            public void run() {

               //Do long operation stuff here search stuff

                try {

                    // code runs in a thread
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mProgressDialog.dismiss();
                        }
                    });
                } catch (final Exception ex) {

                }
            }
        }.start();
N Jay
  • 1,774
  • 1
  • 17
  • 36
  • Thank you so much, I almost got it to work with what you gave me, but how can I update a global value in that thread? I keep getting an error stating that it needs to be final, but when I set it to final, I get another error since the only time I set the value is inside the thread. – swag antiswag Nov 03 '15 at 20:54
  • @swagantiswag make it a class variable and it will work. – N Jay Nov 03 '15 at 21:31
  • Thank you, would you mind telling me why it works if its a class variable and not just a function variable? – swag antiswag Nov 03 '15 at 21:44
  • @swagantiswag read this http://stackoverflow.com/questions/4732544/why-are-only-final-variables-accessible-in-anonymous-class – N Jay Nov 03 '15 at 22:06
  • I used a runnable instead of a thread and then .run() at the end instead of .start() – Sam May 25 '20 at 09:53
0

Use async task for heavy task. Put your progress dialog code in onPreExecute method progress dialog dismiss code in onPostExecute method and all your heavy task in doInBackground method.

-1

try passing down the context on a new class with your progress bar (this goes on your main activity)

NAME_OF_YOUR_CLASS context = new NAME_OF_YOUR_CLASS(getApplicationContext());

and on your class call the method like this..(this goes on class)

 public Networking(Context c) {
        this.context= c;
    }

dont forget to make context a field (private final Context context;)

hope this helps

also idk if this will work but try to extend AsyncTask and use methods to run your progress bar there.

  • There is already a huge problem in his solution! sending a context is easy part but do you seriously think its just alright to show a loading on a new thread in android ? – N Jay Nov 02 '15 at 04:14