Skip to content Skip to sidebar Skip to footer

Android: Show Progress Indicator Of Parse While Downloading A XML

I need to show a progress indicator bar while parsing a downloading xml. I did it downloading first, and then parsing, but I wan't to do both action parallels. This is part of my c

Solution 1:

Try:

public class SomeActivity extends Activity {

   private static final int PROGRESS_DIALOG_ID = 0;

   @Override
   protected Dialog onCreateDialog(int id) {
       if (id == PROGRESS_DIALOG_ID) {
           ProgressDialog dialog = new ProgressDialog(this);
           dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
           dialog.setMessage("Loading...");
           dialog.setCancelable(false);
           return dialog;
       }
       return super.onCreateDialog(id);
   }

   public void someMethod(){
       new SomeTask().execute();
   }

   class SomeTask extends AsyncTask<Void, Void, Void> {

      @Override
      protected void onPreExecute() {
           showDialog(PROGRESS_DIALOG_ID);
      }

      @Override
      protected Void doInBackground(Void... voids) {
        // download and parse
          return null;
      }
      @Override
      protected void onPostExecute(Void aVoid) {
          dismissDialog(PROGRESS_DIALOG_ID);
      }
  }
}

Solution 2:

I suggest you use AsyncTask to do what u r trying. Use the doInBackground() to download and parse() while you can use the onProgressUpdate() to show the progressdialog. here is a tutorial to do the same.

http://www.shubhayu.com/android/witer-asynctask-what-why-how

or you can search for AsyncTask in the Android Developers blog.

EDIT

Declare private ProgressDialog mProgress = null; in your extended AsyncTask and then add the following assuming Progress parameter is String and Result parameter is boolean

@Override
protected void onPreExecute() {
    mProgress = ProgressDialog.show(mContext, progressTitle, mMessage);
    mProgress.setCancelable(false);
    super.onPreExecute();
}

@Override
protected void onProgressUpdate(String... progress) {
    mProgress.setMessage(progress);
}

@Override
protected void onPostExecute(Boolean result) {

    if( result ){
        mProgress.setMessage("Succesfully Downloaded and Parsed!");
    }else{
        mProgress.setMessage("Failed to download :(");
    }
    mProgress.dismiss();
    super.onPostExecute(result);
}

And in your doInBackGround(), use the following to update the progressdialog

publishProgress("your update Message");

Post a Comment for "Android: Show Progress Indicator Of Parse While Downloading A XML"