Search code examples
javaandroidactionbarsherlockprogressdialogprogress

ProgressDialog does not update in ActionBarSherlock


I am not able to see any progress updates in progress dialog.

In the activity class:

void retrieveImages()
{
        mImageUriList = new ArrayList<String>();
        mImageUriList = Utils.fetchImages(this);
        mProgressDialog = new ProgressDialog(this);

        // progress dialog here is class level activity member
        mProgressDialog.show();
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setMessage("Processing Images..");
        mProgressDialog.setMax(100);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        mProgressDialog.setCanceledOnTouchOutside(false);
        new GetImageAsyncTask(this, mImageUriList,mProgressDialog).execute();

        // passing progress dialog to async task
    }

In the AsyncTask

@Override
    protected Void doInBackground(Void... params) 
    {
    int progress_status;
    float count=imageUriList.size();
    float increment=0;
    float p=(increment/count)*100;
        for(int i=0;i<count;i++)
        {
            increment++;
            //do some operation
            p=(increment/count)*100;
            progress_status=Math.round(p);
            publishProgress(progress_status);
            Log.i(TAG, "progress value-"+progress_status);
        }   
        return null;
    }
    @Override
protected void onProgressUpdate(Integer... values) 
{
    //super.onProgressUpdate(values);
    progressDialog.setProgress(values[0]);
}

I can only see spinner and message "Processing images..", i do not see any progress bar/updates

I am using ActionBarSherLock, I also tried this solution here that also does not seems to work, also it just shows progress spinner on action bar. I want a dialog to show progress, not on action bar.


Solution

  • The trick here is not to create progress bar in activity, create progress dialog in the AsyncTask by passing context of Activity into the AsyncTask, take a look at code here

    void retrieveImages()
    {
            //some method in sherlock activity 
            mImageUriList = new ArrayList<String>();
            mImageUriList = Utils.fetchImages(this);
            new GetImageAsyncTask(this, mImageUriList).execute();
            //NOT passing progress dialog to async task,just activity context
    }
    

    In AsyncTask

    In the constructor

    GetImageAsyncTask(Context mContext,ArrayList<String> mImageUriList)
    {
        // Other intializations
        mProgressDialog = new ProgressDialog(mContext);
    
        mProgressDialog = new ProgressDialog(this);
        // progress dialog here is asynctask class member
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setMessage("Processing Images..");
        mProgressDialog.setMax(100);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        mProgressDialog.setCanceledOnTouchOutside(false);
    }
    

    Now onPreExecute(), show/load the progress dialog

    public void onPreExecute()
    {
       //show dialog box on pre execute
       mProgressDialog.show();
    }
    

    Keep the doInBackground and onProgressUpdate the same