Skip to content

ProgressDialog in AsyncTask: Dynamic Update Text/Title

July 17th, 2015 - Software(1 min)

Sometimes, you may want to update the text shown in the progress dialog. If you try to perform this in doInBackground() you’ll get an exception along the lines:

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

The solution is to not do it in doInBackground() but instead trigger onProgressUpdate() and perform the update there. To do this, you’ll need to pass an object. Luckily, the type of object is defined by the second parameter of the declaration AsyncTask<Params, Progress, Result>.

The Progress object can be a String and you can pass a variable number of arguments to the onProgressUpdate() (via publishProgress()).

Share on

A little experiment: