3

I have a method setup that takes about 3-5 seconds to run (because it does something online) and I would like to show a loading dialog, but I cant figure out how to do it, I tried starting the loading dialod before I called the method then tried to dismiss it right after like this:

dialog.show();
myMethod();
dialog.cancel();

But that didn't work, does anyone have any suggestions?

2 Answers 2

8

AsyncTask is my Favouite but you may use Handlers too :)

Invest your time to go through this nice blog.

Below snippet will help you.

    package org.sample;

    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.os.Bundle;
    import android.os.Handler;

    public class Hello extends Activity {

        private Handler handler;
        private ProgressDialog dialog;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            dialog = new ProgressDialog(this);
            dialog.setMessage("Please Wait!!");
            dialog.setCancelable(false);
            dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            dialog.show();

            new Thread() {
                public void run() {
                    // Do operation here.
                    // ...
                    // ...
                    // and then mark handler to notify to main thread 
                    // to  remove  progressbar
                    //
                    // handler.sendEmptyMessage(0);
                    //
                    // Or if you want to access UI elements here then
                    //
                    // runOnUiThread(new Runnable() {
                    //
                    //     public void run() {
                    //         Now here you can interact 
                    //         with ui elemements.
                    //
                    //     }
                    // });
                }
            }.start();

            handler = new Handler() {
                public void handleMessage(android.os.Message msg) {
                    dialog.dismiss();
                };
            };
        }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

I tried using that before but I keep getting "Only the original thread that created a view hierarchy can touch its views." error. And I have no idea what that means... lol... any suggestions?
you are trying to access UI elements from thread.Thats why you are getting this error.
Thanks soo much! It's working perfect now :) I ended up using the the run() like you had it before to do my method, but then using the runOnUiThread to change the views that needed changed! :D
1

Running a long duration task on the UI thread is a "no no". Use AsyncTask to run the long duration tasks in the background and update the UI when it is complete.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.