Show spinning wheel dialog while loading data on Android Show spinning wheel dialog while loading data on Android java java

Show spinning wheel dialog while loading data on Android


So, seeing that this answer is getting more and more upvotes, I have decided that I should take it upon myself to improve the quality of the answer. As I look at the original, I see no real 'answer' to the question of "what is the problem in my code?". I am leaving the original answer intact below to preserve the linking, which is what I am assuming has caused the answers popularity.

Updated Answer

It is likely that you are violating the 'single thread model'. The Android UI Toolkit is not threadsafe, and should only be manipulated from the 'main' UI thread. Android has a handful of helpful methods you can use to ensure that your UI manipulations are done on the UI thread. Most of the details of these method calls can be found in the Android Painless Threading blog post (linked in the word 'one' below, and here for quick reference).

Looking at your code, the specific violation I see is that a ProgressDialog is being created in what is likely the UI thread, which is then dismissed in the newly created background thread.

You would be better off encapsulating your background method in a Runnable and using View#post(Runnable) to do the work in the background.

Remember, there are many ways to do background, so take a look at what is available, and use what feels right for your situation.

Original Answer

Take a look at one of the many tutorials of how to do asynchronous work in Android.

Also, here are some other StackOverflow questions that are similar

Progress dialog problem in Android

Progress Dialog on open activity

Progress Dialog while starting new activity

Android: Progress Dialog spinner not spinning

Updating progress dialog

android: showing a progress dialog


To create.

ProgressDialog dialog = new ProgressDialog(this); // this = YourActivitydialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);dialog.setTitle("Loading");dialog.setMessage("Loading. Please wait...");dialog.setIndeterminate(true);dialog.setCanceledOnTouchOutside(false);

To show.

dialog.show();

To dismiss.

dialog.dismiss();


I had the problem of switching between two activities, Activity A and Activity B. Activity B had long running tasks that was updating the UI, so it was not a candidate for AsyncTask. I solved it by launching a progress dialog on Activity A and launching Activity B before we finished the first activity. The Code Looks like:

            // wrap thread around original code for progress button            final ProgressDialog ringProgressDialog = ProgressDialog.show(getActivity(), "Working", "Please Wait",                    true, false);            ringProgressDialog.setIndeterminate(true);            new Thread(new Runnable() {                public void run() {                    try {                        Intent startNewActivityOpen = new Intent(getActivity(), ActivityB.class);                        startNewActivityOpen.setFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);                        startActivity(startNewActivityOpen);                        GlobalVars.navigateFromScreen = true;                        Thread.sleep(3000);                        if (ringProgressDialog != null && ringProgressDialog.isShowing()) {                            ringProgressDialog.dismiss();                        }                        getActivity().finish();                    } catch (Exception e) {                    }                }            }).start();