How to show progress dialog in Android?

后端 未结 16 2890
予麋鹿
予麋鹿 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条回答
  •  -上瘾入骨i
    2020-11-28 08:43

    Point one you should remember when it comes to Progress dialog is that you should run it in a separate thread. If you run it in your UI thread you'll see no dialog.

    If you are new to Android Threading then you should learn about AsyncTask. Which helps you to implement a painless Threads.

    sample code

    private class CheckTypesTask extends AsyncTask{
            ProgressDialog asyncDialog = new ProgressDialog(IncidentFormActivity.this);
            String typeStatus;
    
    
            @Override
            protected void onPreExecute() {
                //set message of the dialog
                asyncDialog.setMessage(getString(R.string.loadingtype));
                //show dialog
                asyncDialog.show();
                super.onPreExecute();
            }
    
            @Override
            protected Void doInBackground(Void... arg0) {
    
                //don't touch dialog here it'll break the application
                //do some lengthy stuff like calling login webservice
    
                return null;
            }
    
            @Override
            protected void onPostExecute(Void result) {
                //hide the dialog
                asyncDialog.dismiss();
    
                super.onPostExecute(result);
            }
    
    }
    

    Good luck.

提交回复
热议问题