Wednesday, August 11, 2010

show ProgressBar while processing in background in ANDROID

AIM: To show progress bar while doing work in background thread like downloading image/vedio/response etc..

Solution:
You can use AsyncTask   class which will do the background work in background thread while showing another UI(like progressbar,textview with loading or any other layout what ever you like) in UI thread.

You can do this by using the following code snippet.

Suppose you are downloading any thing from internet .here i am sending request to server and reading the response from the server.

The AsyncTask class which will does the background task.

       // ASync Task Begin to perform Billing information
private class performBackgroundTask extends Async Task < Void, Void, Void >  {

                private ProgressDialog Dialog = new ProgressDialog( this);

protected void onPreExecute() {
                       // here you have place code which you want to show in UI thread like progressbar or dialog or any layout . here  i am displaying a progressDialog with test please wait while loading......

                        Dialog.setMessage(" please wait while loading............");
Dialog.show();

              
}

protected Void doInBackground(Void... params) {
 // write here the code to download  or any background task.
                         DownloadVideo();  // example call method which will download vedio .
return null;
}

protected void onPostExecute(Void unused) {
                       Dialog.dismiss();    
// the background task is completed .You can do the code here for next things

}

}

Call this method with the following line whereever you wants .for example like oncreate() or in anybutton handler as follows:
 new performBackgroundTask().execute();

Thats it.by this way we can solve the problem.


Android Developers Blog

Ram's shared items