How do I show and then remove an android progress dialog

前端 未结 3 1070
遇见更好的自我
遇见更好的自我 2020-12-11 21:47

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

pd = ProgressDialog.show(myActivity.this, \"\", \"Loading. Please wait...\", true);
相关标签:
3条回答
  • 2020-12-11 22:29

    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();
                }
         }
         };
         };
    
    
    
    }
    
    0 讨论(0)
  • 2020-12-11 22:46

    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();
        }
    
    }
    
    0 讨论(0)
  • 2020-12-11 22:46

    To remove a progressBar

    ProgressBar progressbar = (ProgressBar)findViewById(R.id.progressbarID);
    
    progressBar.setVisibility(View.INVISIBLE) ;
    

    hide the progressbar in xml and show it like

     ProgressBar progressbar = (ProgressBar)findViewById(R.id.progressbarID);
    
        progressBar.setVisibility(View.VISIBLE) ;
    
    0 讨论(0)
提交回复
热议问题