Fetching data from Internet in background and showing ProgressDialog or ProgressBar

I am developing an application which require accessing a website for data, and will show that data on device. I wants to fetch data from Internet in background and show ProgressDialog or ProgressBar on device and when application receive response from server app will dismiss the dialog or bar and will show data .

For this i am using AsyncTask -

code for AsyncTask is as follows--

ServerTask extends AsyncTask {
        protected void onPreExecute() {
                dialogAccessingServer = new ProgressDialog(ctx);

        protected ServerResponse doInBackground(String... urlArray) {

                String urlString = urlArray[0];
                HttpResponse serverResponseObject = null;

                //finding HttpResponse

                return serverResponseObject;

        }//end of doInBackground

        protected void onPostExecute(HttpResponse serverResponseObject){



and calling this code as follows--

ServerTask serverTaskObject = new ServerTask();
HttpResponse response = serverTaskObject.get();

//performing operation on response

but ProgressDialog is not shown.(I guess the reason for it is the thread is not complete and android invalidate only when thread has completed).

My Questions -- 1- If my guess is right ? If yes then how I should implement it? 2- If there is any other better way to do this?


Asked by: Catherine693 | Posted: 25-01-2022

Answer 1

Following is a template code that displays a ProgressDialog while a task is executing in background:

class GetTask extends AsyncTask<Object, Void, String>
        Context mContext;
            ProgressDialog mDialog = null;

            GetPhotoFeedTask(Context context)
        mContext = context;

        protected void onPreExecute()

             mDialog = new ProgressDialog(mContext);
             mDialog.setMessage("Please wait...");
        protected String doInBackground(Object... params)
                  // do stuff in background : fetch response

        protected void onPostExecute(String result)
            // mDialog.dismiss();

and you invoke it from your activity using new GetTask(this).execute() statement;

Note: Note that while displaying a ProgressDialog if the user switches the Orientation or causes event that ensues one, the code might break. It is advised to use Managed Dialogs for such cases.

Answered by: Edward102 | Posted: 26-02-2022

Answer 2

If there is some pending work on UI thread 'Progress dialog' will not appear, so dialog.show() should be the last line on UI thread and any further work should be done in onPostExecute() method.

Answered by: Dexter712 | Posted: 26-02-2022

Similar questions

android - I want a progressbar but get a spinner progressdialog

I am using a public AsynTask to download data, and I am trying to show a progress bar which would show the download progress. I think I have the code right, but all I get is a spinner progressdialog. Am I missing something? Why isn't the progress bar showing up? Here is the code. Thanks for any pointers. public class FileDownloader extends AsyncTask&lt;String, Integer, Void&gt; { private Context _appContext...

How To Add Second ProgressBar Android ProgressDialog

Friends; How To Set ProgressDialog Second Progress Bar Like Below Images. thanx.

progressdialog - Android: error while trying to show a progressbar in asynctask

Working with Android I've got an error while I try to show a progressbar in an asynctask. I have the error at the bold line. The variable context is the context of the app, I'm sure. Here there's the code: public class UpdateDBTask extends AsyncTask { ProgressDialog progressDialogListFiles; ProgressDialog progressDialogUpdateDB; protected void onPreExecute () { progressDialogUpdateDB =...

Android: ProgressBar in ProgressDialog not Animating

I have created one AsyncTask class for fetching data from background (Accessing database). In onPreExecute() method, I create one progress dialog try { progressDialog = ProgressDialog.show(context, "", "Please wait...", true); progressDialog.setIndeterminate(true); } catch (final Throwable th) { // TODO ...

progressdialog - How to use transferTo and using progressbar in android

When I copy file-A to folder-B in Android, I want use ProgressDialog(ProgressDialog.STYLE_HORIZONTAL). Example 1: String mStart = "/sdcard/aaaa/"; FileInputStream fis = new FileInputStream(mStart); FileOutputStream fos = new FileOutputStream(path); FileChannel fin = fis.getChannel(); FileChannel fout = fos.getChannel(); File f = new File(mStart); fin.tr...

android - What is the difference between Progressbar and progressDialog?

I have searched everywhere and read the official doc of Google. But I still don't see the difference between them. When should we use ProgressBar and when should we use ProgressDialog?

java - Android make a progressDialog ProgressBar Invisible

This is my ProgressDialog progressBar = new ProgressDialog(Wallpapers.this); progressBar.setCancelable(false); progressBar.setMessage("Downloading " + downloadedFile + ".png"); progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressBar.setProgress(0); progressBar.setMax(100); progressBar.setButton(DialogInterface.BUTTON_NEGATIVE, "No", new DialogInterface.OnClickListener() { @Override ...

progressdialog - infinite progressbar in android

I want to set the message with big font in Progress dialog in android. I am using following code to do so, progress_dialog=new ProgressDialog(this); progress_dialog.show(About_Us_Activity.this, Html.fromHtml(""), Html.fromHtml("big&gt;Message...&lt;/big&gt;"), false); i used the above code in AsyncTask onPreExecute() method and dismissed on onPostExecute method, the issue is tha...

progressdialog - Android- How to use another layout as progressbar

I've seen this in lots of app , they don't use ProgressDialog because it doesn't let us to do anything in the app.I don't want that . I've seen it in another app ,instead of ProgressDialog , they make the ui to show a progressbar and also we can do other work either, for example the whole ui become a progressbar and when it's done, the main ui loads . I think they use another layout when progress...

progressdialog - How to display Progressbar in Android Application Class?

Dear Folks, I am running to a difficulty where I am trying to display the ProgresDialog from the Android class which extends application. I am currently doing context=getApplicationContext(); dialog=ProgressDialog.show(context, "Status", "Downloading The master"); But it throws me the following exception Unable to add window -- token null is ...

progressdialog - Android Positioning Progress Dialog or Custom Progress Dialog

I don't know how to position the progress dialog(the one with the rotating image). When my application starts its display an full screen image and a progress dialog box. I need to moved the progress dialog box a little lower. Thanks Rajesh

progressdialog - Android: How to Create a Modal Progress "Wheel" Overlay?

I would like to show a modal progress "wheel" overlay on my view. The ProgressDialog comes close, but I do not want the dialog background or border. I tried setting the background drawable of the dialog window: this.progressDialog = new ProgressDialog(Main.this); this.progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0)); this.progressDialog.setProgressStyle(ProgressDialog....

android - ProgressDialog not working in external AsyncTask

I'm beginning to think that to get a ProgressDialog to work the AsyncTask has to be an inner class within an Activity class. True? [Edited much later...the answer is False and I'm not sure if this is a bug or what. I'm using Android 1.5. I'm going to read up on Services.] I have an activity the uses a database to manipulate information. If the database is populated all is well. If it is not populated then I ne...

dialog - Prevent ProgressDialog from being dismissed when I click the search button (Android)

In a long-running operation, I'm showing a popup dialog (created from ProgressDialog to prevent other operations from happening). I have made it non-cancellable with setCancelable(false), so I can't close it using the back button, but surprisingly, the Search hardware button dismisses the dialog! More exactly, the global search application is displayed, and when I come back to my a...

android - ProgressDialog does not display until after AsyncTask completes

I am trying to display an indefinite ProgressDialog, while an AsyncTask binds to a RemoteService. The RemoteService builds a list of the users contacts when the service is first created. For a long list of contacts this may take 5~10 seconds. The problem I am having, is that the ProgressDialog does not display until after the RemoteService has built it's list of contacts. I even tried putting a Thread.sleep in t...

Android ASync task ProgressDialog isn't showing until background thread finishes

I've got an Android activity which grabs an RSS feed from a URL, and uses the SAX parser to stick each item from the XML into an array. This all works fine but, as expected, takes a bit of time, so I want to use AsyncActivity to do it in the background. My code is as follows: class AddTask extends AsyncTask&lt;Void, Item, Void&gt; { protected void onPreExecute() { pDialog = ProgressDialog.show(...

Android: ProgressDialog doesn't show

I'm trying to create a ProgressDialog for an Android-App (just a simple one showing the user that stuff is happening, no buttons or anything) but I can't get it right. I've been through forums and tutorials as well as the Sample-Code that comes with the SDK, but to no avail. This is what I got: btnSubmit.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { ...

android - ProgressDialog - how to remove the numbers

I was following the progress dialog example in the ApiDemos. all went great except for one thing - I want to remove the numbers that appear underneath the bar (those running numbers that run from 0 to .getMax(). couldn't find how to do it. anyone? Ori

android - How do I get a ProgressDialog to show on application Startup?

aka, what is wrong with this code:- package com.mez.appofjaq; import com.mez.appofjaq.RssParser.RssFeed; import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.webkit.WebView; public class AppOfJaq extends Activity { protected static final int REFRESH = 0; String streamTitle = ""; protect...

android - ProgressDialog onCreate

In "onCreate" I'm downloading data from web.The duration of downloading data is 10 sec. I wan't to have ProgressDialog while the data is downloading. Here is my code , but the ProgressDialog doesn't appear: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ProgressDialog dialog = ProgressDialog.show(Test.this, "", "Load...

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android