How to show progress dialog in Android?

后端 未结 16 2588
予麋鹿
予麋鹿 2020-11-28 07:40

I want to show ProgressDialog when I click on Login button and it takes time to move to another page. How can I do this?

相关标签:
16条回答
  • 2020-11-28 08:17

    You better try with AsyncTask

    Sample code -

    private class YourAsyncTask extends AsyncTask<Void, Void, Void> {
        private ProgressDialog dialog;
    
        public YourAsyncTask(MyMainActivity activity) {
            dialog = new ProgressDialog(activity);
        }
    
        @Override
        protected void onPreExecute() {
            dialog.setMessage("Doing something, please wait.");
            dialog.show();
        }
        @Override
        protected Void doInBackground(Void... args) {
            // do background work here
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
             // do UI work here
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    }
    

    Use the above code in your Login Button Activity. And, do the stuff in doInBackground and onPostExecute

    Update:

    ProgressDialog is integrated with AsyncTask as you said your task takes time for processing.

    Update:

    ProgressDialog class was deprecated as of API 26

    0 讨论(0)
  • 2020-11-28 08:17
            final ProgressDialog progDailog = ProgressDialog.show(Inishlog.this, contentTitle, "even geduld aub....", true);//please wait....
    
            final Handler handler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    Barcode_edit.setText("");
                    showAlert("Product detail saved.");
    
    
                }
    
            };
    
            new Thread() {
                public void run() {
                    try {
             } catch (Exception e) {
    
                    }
                    handler.sendEmptyMessage(0);
                    progDailog.dismiss();
                }
            }.start();
    
    0 讨论(0)
  • 2020-11-28 08:18

    ProgressDialog is now officially deprecated in Android O. I use DelayedProgressDialog from https://github.com/Q115/DelayedProgressDialog to get the job done.

    Usage:

    DelayedProgressDialog progressDialog = new DelayedProgressDialog();
    progressDialog.show(getSupportFragmentManager(), "tag");
    
    0 讨论(0)
  • 2020-11-28 08:20

    when you call in oncreate()

    new LoginAsyncTask ().execute();
    

    Here how to use in flow..

    ProgressDialog progressDialog;
    
      private class LoginAsyncTask extends AsyncTask<Void, Void, Void> {
      @Override
        protected void onPreExecute() {
            progressDialog= new ProgressDialog(MainActivity.this);
            progressDialog.setMessage("Please wait...");
            progressDialog.show();
            super.onPreExecute();
        }
    
         protected Void doInBackground(Void... args) {
            // Parsse response data
            return null;
         }
    
         protected void onPostExecute(Void result) {
            if (progressDialog.isShowing())
                            progressDialog.dismiss();
            //move activity
            super.onPostExecute(result);
         }
     }
    
    0 讨论(0)
  • 2020-11-28 08:21

    This is the good way to use dialog

    private class YourAsyncTask extends AsyncTask<Void, Void, Void> {
    
       ProgressDialog dialog = new ProgressDialog(IncidentFormActivity.this);
    
       @Override
        protected void onPreExecute() {
            //set message of the dialog
            dialog.setMessage("Loading...");
            //show dialog
            dialog.show();
            super.onPreExecute();
        }
    
       protected Void doInBackground(Void... args) {
        // do background work here
        return null;
       }
    
       protected void onPostExecute(Void result) {
         // do UI work here
         if(dialog != null && dialog.isShowing()){
           dialog.dismiss()
         }
    
      }
    }
    
    0 讨论(0)
  • 2020-11-28 08:21
    ProgressDialog dialog = 
       ProgressDialog.show(yourActivity.this, "", "Please Wait...");
    
    0 讨论(0)
提交回复
热议问题