Android Progress Dialog

My application retrieves some html code from the Internet and, when done, displays it on the device screen. Since this takes about 3-4 seconds, at this time the screen remains black, I would like to use the 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 does not display anything else. I have one error in logcat:

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

could you help me? Thanks in advance.

+6
source share
5 answers

The error is quite explicit. To update a single visual object, you must run the changes inside the main thread. A quick and dirty fix can cause the update code inside runOnUiThread () .

However, in your case, I would use AsyncTask to download and update the progress of the progress bar. A task has the property of starting a user interface thread at its completion (so that you can update views there, for example, reject the execution dialog)

Here's an example of using AsyncTask to display a download progress dialog box.

Update

Stackoverflow already has answers to all your questions. Here's an AsyncTask example for loading some content and displaying the loading process. Just what you want.

Update 2

Ok, here is your code using 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(); } } 
+17
source

You need to do it

 runOnUiThread(new Runnable() { public void run() { // Do Your Stuff }}); 
+4
source

Turn off your dialog as follows:

 Handler handler = new Handler(); handler.post(new Runnable(){ public void run(){ progressDialog.dismiss(); } }); 
+2
source

Create a UI thread after network operation completes

 runOnUiThread(new Runnable() { @Override public void run() { progressDialog.dismiss(); } }); 
+1
source

The top answer works fine, here is an example of AsyncTask implementation in MonoDroid (thanks to Greg Shekel): http://mono-for-android.1047100.n5.nabble.com/AsyncTask-td4346647.html

0
source

Source: https://habr.com/ru/post/898970/


All Articles