Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show progress dialog in Android?

ProgressDialog pd = new ProgressDialog(yourActivity.this);
pd.setMessage("loading");
pd.show();

And that's all you need.


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


To use ProgressDialog use the below code

ProgressDialog progressdialog = new ProgressDialog(getApplicationContext());
progressdialog.setMessage("Please Wait....");

To start the ProgressDialog use

progressdialog.show();

progressdialog.setCancelable(false); is used so that ProgressDialog cannot be cancelled until the work is done.

To stop the ProgressDialog use this code (when your work is finished):

progressdialog.dismiss();`

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<Void, Void, Void>{
        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.


Simple coding in your activity like below:

private ProgressDialog dialog = new ProgressDialog(YourActivity.this);    
dialog.setMessage("please wait...");
dialog.show();
dialog.dismiss();

Declare your progress dialog:

ProgressDialog progressDialog;  

To start the progress dialog:

progressDialog = ProgressDialog.show(this, "","Please Wait...", true);  

To dismiss the Progress Dialog :

 progressDialog.dismiss();

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");