How To Update The Text In The Ui From The Background Thread In Android
Solution 1:
Put your Ui method inside runonUiTHREAD like this
runOnUiThread(new Runnable() {
publicvoidrun() {
tv.setText("ABC");
}
});
Solution 2:
In AsyncTask, onPostExecute() and onPreExecute() both runs on UI thread. So you can change the text in onPostExecute() method.
Or you can also call runOnUiThread in doInBackground() method which runs in thread:
runOnUiThread(new Runnable() {
publicvoidrun() {
// change text
}
});
It post runnable to run on UI thread.
Solution 3:
I guess the short answer is, yes, you can update UI elements in the onProgressUpdate method. OnProgressUpdate is actually invoked on the UI thread itself, so you don't need to do anything fancy.
How do you know your onProgressUpdate isn't working if it's hardcoded to "Wait background work is going on"?
Also, is there any reason why you aren't using a ProgressDialog to show the "Wait background work is going on" message to your users? It's typically more intuitive if you actually want them to wait. It displays a spinner or progress bar (your choice) that lets them know work is being done. It also doubles up as a way of preventing them from doing other stuff until your application is done processing whatever it has to.
Post a Comment for "How To Update The Text In The Ui From The Background Thread In Android"