Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I show and then remove an android progress dialog

I can get a progress bar to appear with the following code

pd = ProgressDialog.show(myActivity.this, "", "Loading. Please wait...", true);

straight forward, but once I have the code execute I want it to go away, but when I run the dismiss method after I never see the dialog box show at all.

Heres the code in context which is wrapped in oncreate

pd = ProgressDialog.show(myActivity.this, "", "Loading. Please wait...", true);
runCode();  
setListAdapter(new CustomAdapter(myActivity.this)); 
pd.dismiss();

I thought you can show/dismiss progress dialog's anywhere in the activity but I must be wrong.

like image 308
Brian Avatar asked Dec 22 '22 20:12

Brian


2 Answers

refer the sample code sinnpet of mine,hope this may help you

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ProgressBar;

public class PlayActivity extends Activity {
    /** Called when the activity is first created. */

  private ProgressBar mProgress;
     private int mProgressStatus = 0;
     private int count=0;
     private Handler mHandler=new Handler();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mProgress = (ProgressBar) findViewById(R.id.ProgressBar01);
        new Thread(myThread).start();

    }
    private Runnable myThread = new Runnable(){

     @Override
     public void run() {
     // TODO Auto-generated method stub
     while (mProgressStatus<=100){
     try{
     myHandle.sendMessage(myHandle.obtainMessage());
     Thread.sleep(1000);
     }
     catch(Throwable t){
     }
     }
     }

     Handler myHandle = new Handler(){

     @Override
     public void handleMessage(Message msg) {
     // TODO Auto-generated method stub
      mProgressStatus=mProgressStatus+10;
      count=mProgressStatus;
      Log.d("mProgressStatus",Integer.toString(count));
      mProgress.setProgress(mProgressStatus);
      if(count > 80)
            {
             Log.d("mProgressStatus",Integer.toString(mProgressStatus));
             counter.start();
            }
     }
     };
     };



}
like image 38
Sankar Ganesh PMP Avatar answered Dec 24 '22 09:12

Sankar Ganesh PMP


here is the code that I got to work

private class UpdateFeedTask extends AsyncTask<MyActivity, Void, Void> {

    private ProgressDialog mDialog;

    protected void onPreExecute() {
        Log.d(TAG, " pre execute async");
        mDialog = ProgressDialog.show(MyActivity.this,"Please wait...", "Retrieving data ...", true);

    }

    protected void onProgressUpdate(Void... progress) {
        Log.d(TAG, " progress async");     
    }

    @Override
    protected Void doInBackground(MyActivity... params) {
        // TODO Auto-generated method stub
        return null;
    }

    protected void onPostExecute(Void result) {
        Log.d(TAG, " post execute async");
        mDialog.dismiss();
    }

}
like image 153
Brian Avatar answered Dec 24 '22 10:12

Brian