Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android progress dialog

My application fetches some html code from the internet and when done , displays it on the devices screen. Since it takes about 3-4 seconds to do that , in this time the screen stays black , I'd like to use a progress dialog. This is my code :

package com.nextlogic.golfnews;



// ALL THE IMPORTS ....

public class Activity1 extends Activity {

    private ProgressDialog progressDialog;
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);

        progressDialog = ProgressDialog.show(Activity1.this, "", "Loading...");
        new Thread() 
        {
          public void run() 
          {

             try
               {
                sleep(2000);

          // HERE I'VE PUT ALL THE FUNCTIONS THAT WORK FOR ME
 }
    catch (Exception e)
    {
        Log.e("tag",e.getMessage());
    }
// dismiss the progressdialog   
  progressDialog.dismiss();
 }
}.start();

The program works but it doesn't display anything anymore. I have one error in logcat :

Only the original thread that created a view hierarchy can touch its views.

Could you please help me ? Thanks in advance.

like image 981
Teo Avatar asked Oct 10 '11 13:10

Teo


People also ask

What is progress dialog in android?

ProgressDialog is a modal dialog, which prevents the user from interacting with the app. Instead of using this class, you should use a progress indicator like ProgressBar , which can be embedded in your app's UI. Alternatively, you can use a notification to inform the user of the task's progress.

How can I get progress bar in android?

In android there is a class called ProgressDialog that allows you to create progress bar. In order to do this, you need to instantiate an object of this class. Its syntax is. ProgressDialog progress = new ProgressDialog(this);

What is progress bar in android?

Android ProgressBar is a graphical view indicator that shows some progress. Android progress bar displays a bar representing the completing of the task. Progress bar in android is useful since it gives the user an idea of time to finish its task.


3 Answers

The error is explicative enough. To update one visual object you must run the changes inside main thread. A quick and dirty fix could be calling the update code inside runOnUiThread().

However in your case I would use an AsyncTask to download and update the progress of the progress bar. The task has the property to run on UI thread when it ends (so you can update the views there, such as dismissing the progress dialog)

Here is an example how to use an AsyncTask to display a download progress dialog.

Update

Stackoverflow already has the answers to all your question. Here is an example of an AsyncTask to download some content and display the download progress. Just what you want.

Update 2

Ok here is your code using an AsyncTask:

public class Activity1 extends Activity
{
    private ProgressDialog progressDialog;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        new AsyncTask<Integer, Integer, Boolean>()
        {
            ProgressDialog progressDialog;

            @Override
            protected void onPreExecute()
            {
                /*
                 * This is executed on UI thread before doInBackground(). It is
                 * the perfect place to show the progress dialog.
                 */
                progressDialog = ProgressDialog.show(Activity1.this, "",
                        "Loading...");
            }

            @Override
            protected Boolean doInBackground(Integer... params)
            {
                if (params == null)
                {
                    return false;
                }
                try
                {
                    /*
                     * This is run on a background thread, so we can sleep here
                     * or do whatever we want without blocking UI thread. A more
                     * advanced use would download chunks of fixed size and call
                     * publishProgress();
                     */
                    Thread.sleep(params[0]);
                    // HERE I'VE PUT ALL THE FUNCTIONS THAT WORK FOR ME
                }
                catch (Exception e)
                {
                    Log.e("tag", e.getMessage());
                    /*
                     * The task failed
                     */
                    return false;
                }

                /*
                 * The task succeeded
                 */
                return true;
            }

            @Override
            protected void onPostExecute(Boolean result)
            {
                progressDialog.dismiss();
                /*
                 * Update here your view objects with content from download. It
                 * is save to dismiss dialogs, update views, etc., since we are
                 * working on UI thread.
                 */
                AlertDialog.Builder b = new AlertDialog.Builder(Activity1.this);
                b.setTitle(android.R.string.dialog_alert_title);
                if (result)
                {
                    b.setMessage("Download succeeded");
                }
                else
                {
                    b.setMessage("Download failed");
                }
                b.setPositiveButton(getString(android.R.string.ok),
                        new DialogInterface.OnClickListener()
                        {

                            @Override
                            public void onClick(DialogInterface dlg, int arg1)
                            {
                                dlg.dismiss();
                            }
                        });
                b.create().show();
            }
        }.execute(2000);

        new Thread()
        {
            @Override
            public void run()
            {

                // dismiss the progressdialog
                progressDialog.dismiss();
            }
        }.start();
    }
}
like image 173
Fernando Miguélez Avatar answered Sep 30 '22 12:09

Fernando Miguélez


You need to do this way

runOnUiThread(new Runnable() {
    public void run() {
      // Do Your Stuff
    }});
like image 44
MKJParekh Avatar answered Sep 30 '22 13:09

MKJParekh


Dismiss your dialog like this:

Handler handler = new Handler();
handler.post(new Runnable(){
   public void run(){
      progressDialog.dismiss();
   }
});
like image 28
Vineet Shukla Avatar answered Sep 30 '22 13:09

Vineet Shukla