3

I have an Android app and I need to change UI dynamically:
ex: when user presses a button, I want to change the current activity's views and, as in some cases there are a lot of views involved, I need to display a ProgressDialog.

I have been using an AsyncTask, but I think that it is not the best solution, because AsyncTask's doInBackground runs a different thread, so I can't update UI from there - I have to use runOnUiThread, which runs the code in the UI Thread, but freezes the progress dialog that is shown.

I guess there should be more people with similar issues, as updating UI while displaying a ProgressDialog seems to be something done regularly by applications.

So does anyone have a solution to this?

pandre
  • 6,685
  • 7
  • 42
  • 50

2 Answers2

2

You can use something like this:

Create a Handler:

 final Handler handler = new Handler();
        final Runnable updateResults = new Runnable() {
            public void run() {
                updateResultsInUi();
            }
        };  

and whenever you want to update UI., you can use wherever you want

handler.post(updateResults);

and In updateResultsInUi method you can do the UI stuff:

 private void updateResultsInUi() {
  // do stuff
 }

Hope it helps

Uday
  • 5,933
  • 9
  • 44
  • 76
0

oh you are completely and utterly wrong.. you can use the asynctask for this purpose. and in my view, it is the best thing missing out in Blackberry and iPhone. but the implementation is a little tricky so please with it. Have a look at this example1 and example2. that will do the trick for you. if you still need help implementing these then get back to me we can solve it for your specific case.

medampudi
  • 399
  • 3
  • 15
  • Hey, I have looked to both examples you gave and it seems like they only teach how to use AsyncTask. I already know that :) What I would like to know is **how to change UI and show a progress dialog while that is happening** – pandre Nov 24 '11 at 11:27