Sebastien FERRAND
Sebastien FERRAND

Reputation: 2150

Android execute code while showing progress dialog

protected void showSpinner() {
    dialogSpin = ProgressDialog.show(activity, "", 
            "Loading. Please wait...", true);

    Thread t = new Thread(new Runnable(){

        @Override
        public void run() {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {


                    EverydayNotesAndroid3Activity.importAllCalendar();
                }
            });
        }

    });
    t.start();
}

I want to show the spinner while the thread is running. This way, I can show how my import of calendar is going to the user while I'm importing it.

Problem : my Spinner dialog will only show when the import is done. I don't know what I'm doing wrong, because according to my code it should show the dialog and then run the import (which takes 30 seconds), but once more, the dialog wait the end of the import to show up.

Thanks for paying attention, ask for more info.

Upvotes: 0

Views: 218

Answers (3)

Janusz
Janusz

Reputation: 189504

I would use an AsyncTask to import the calendar. AsyncTask has three methods that can be overridden. You overwrite doInBackground and do all the stuff that is done in the background thread there. You can call publishProgress from your background task every time something should change in the UI. The AsyncTask will then call onProgressUpdate in the UI-Thread to allow you to show the progress to the user.

Upvotes: 1

sajattack
sajattack

Reputation: 813

Consider looking into Asynctasks.

Upvotes: 2

Sankar
Sankar

Reputation: 1691

You are importing calender in UI thread it self. Then your progress dialog wait's until import has been done. Then again it will show.

If you want to display progress dialog, then import calender in another thread not in UI thread.

Upvotes: 1

Related Questions