vampireLady
vampireLady

Reputation: 137

Android: ProgressDialog not spinning

I am having problems getting the ProgressDialog wheel spinning. Here is my code:

final ProgressDialog dialog = ProgressDialog.show(this, "", "Loading...", true, false);
Thread thread=new Thread(new Runnable(){
    public void run(){
    runOnUiThread(new Runnable(){
        @Override
        public void run() {
            if(dialog.isShowing())
                // starts a foreground service, does database stuff, 
                // sets up a spinner with values
                dialog.dismiss();
            }

        });
    }
});

thread.start();

Everything goes as planned, I get the ProgressDialog, stuff happens in the background and once set, ProgressDialog goes away - the only problem is that the animation in ProgressDialog is not spinning, pretty much rendering it useless.

What am I doing wrong?

Upvotes: 0

Views: 1888

Answers (3)

nobjta_9x_tq
nobjta_9x_tq

Reputation: 1241

Because you put the processing dialog in to wrong area, for example with my error: I have a two activities: MainActivity and ShowingActivity, MainActivity will show processing dialog after new intent tranfer to ShowingActivity and ShowingActivity will get some DATA from server (it will be blocked here). When I call ProcessingDialog in MainActivity, it showed but not spinning because my Intent move to ShowingActivity and it block Ui because action of get DATA from server must wait some seconds. So I fixed it: 1/ in MainActivity call:

final Intent working = new Intent(getApplicationContext(),
                    WorkingActivity.class);
final ProgressDialog ringProgressDialog = ProgressDialog.show(
                    MainActivity.this, "Please wait ...", "Connecting ...",
                    true);
ringProgressDialog.setCancelable(false);
new Thread(new Runnable() {
                        @Override
                        public void run() {
                            //Menthod need time to load:
                                    getDatafromServer();
                             if (BookListFragment.isLoaded) {
                                ringProgressDialog.dismiss();
                                startActivity(working);
                                return;
                             }
});

So it will not block Ui because MainActivity still runing after DATA was gotten. And in ShowingActivity I will use this DATA (because I set DATA is a static String).

Upvotes: 0

Anh Duy
Anh Duy

Reputation: 1183

ProgressDialog example using handler android

    final ProgressDialog dialog = ProgressDialog.show(this, "Title",
            "Message", true);

    final Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            dialog.dismiss();
        }
    };
    Thread t = new Thread() {
        public void run() {
            // write process code here.

            handler.sendEmptyMessage(0);
        }
    };
    t.start();

Fix issue: ProgressDialog not working

Upvotes: 0

Hong Duan
Hong Duan

Reputation: 4304

The code you omitted here

        // starts a foreground service, does database stuff, 
        // sets up a spinner with values

must do something that block the UI thread. Just put them outside the runOnUiThread() Method.

Thread thread=new Thread(new Runnable(){
    public void run(){
        // starts a foreground service, does database stuff, 
        // sets up a spinner with values
        runOnUiThread(new Runnable(){
            @Override
            public void run() {
            if(dialog.isShowing())
                dialog.dismiss();
            }

        });
    }
});

Upvotes: 1

Related Questions