How to show progress dialog in Android?
Asked Answered
M

18

67

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?

Mcginley answered 4/5, 2012 at 9:22 Comment(2)
developer.android.com/guide/topics/ui/dialogs.htmlVindication
Please note: ProgressDialog class was deprecated in API 26 (Oreo). Instead of using this class, you should use a progress indicator like ProgressBar, which can be embedded in your app's UI. Alternatively, you can use a notification to inform the user of the task's progress. Follow the Material design guidelines for Progress & Activity.Biz
E
71

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

Endoenzyme answered 4/5, 2012 at 9:31 Comment(3)
He want to show progress dialog,not want use AsyncTask i thinkSalinometer
@AmeerHamza Updated the answer.Endoenzyme
Anyway, AysncTask is deprecated. Kindly update your answer.Pacian
F
114
ProgressDialog pd = new ProgressDialog(yourActivity.this);
pd.setMessage("loading");
pd.show();

And that's all you need.

Fingered answered 4/5, 2012 at 9:25 Comment(2)
ProgressDialog pd = new ProgressDialog(yourActivity.this); pd.setMessage("loading"); pd.show();now showing anything.what to do.Mcginley
You will need to use your actual Activity rather than the yourActivity.Fingered
E
71

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

Endoenzyme answered 4/5, 2012 at 9:31 Comment(3)
He want to show progress dialog,not want use AsyncTask i thinkSalinometer
@AmeerHamza Updated the answer.Endoenzyme
Anyway, AysncTask is deprecated. Kindly update your answer.Pacian
B
30

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();`
Bashan answered 24/6, 2016 at 10:46 Comment(0)
S
20

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.

Subprincipal answered 4/5, 2012 at 9:22 Comment(2)
Your note that "progress dialog won't work on main UI thread" was what made my day. Thank you.Wight
OnPreExecute and onPostExecute is run on the main thread. And you are starting and stoping the progress dialog from the main thread.Ong
P
8

Simple coding in your activity like below:

private ProgressDialog dialog = new ProgressDialog(YourActivity.this);    
dialog.setMessage("please wait...");
dialog.show();
dialog.dismiss();
Psychotomimetic answered 3/10, 2018 at 9:44 Comment(0)
S
6

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();
Supererogatory answered 22/7, 2014 at 5:36 Comment(0)
R
3

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");
Razorbill answered 17/9, 2017 at 8:20 Comment(0)
R
2

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()
     }

  }
}
Restless answered 5/4, 2014 at 9:44 Comment(0)
T
2

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);
     }
 }
Trinia answered 24/6, 2016 at 10:57 Comment(0)
R
1
ProgressDialog dialog = 
   ProgressDialog.show(yourActivity.this, "", "Please Wait...");
Rudder answered 4/5, 2012 at 9:31 Comment(0)
I
1
ProgressDialog pd = new ProgressDialog(yourActivity.this);
pd.show();
Inexpedient answered 18/4, 2015 at 7:46 Comment(1)
While this does provide a direct answer to the question, this would typically considered low quality on Stack Overflow because it provides no context or explanation. Please consider expanding your answer.Busy
J
1

Step 1:Creata a XML File

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <Button
        android:id="@+id/btnProgress"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Progress Dialog"/>
</LinearLayout>

Step 2:Create a SampleActivity.java

package com.scancode.acutesoft.telephonymanagerapp;


import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class SampleActivity extends Activity implements View.OnClickListener {
    Button btnProgress;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnProgress = (Button) findViewById(R.id.btnProgress);
        btnProgress.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        final ProgressDialog progressDialog = new ProgressDialog(SampleActivity.this);
        progressDialog.setMessage("Please wait data is Processing");
        progressDialog.show();

//        After 2 Seconds i dismiss progress Dialog

        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    Thread.sleep(2000);
                    if (progressDialog.isShowing())
                        progressDialog.dismiss();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}
Janus answered 18/4, 2016 at 11:17 Comment(0)
C
1
  final ProgressDialog loadingDialog = ProgressDialog.show(context,
     "Fetching BloodBank List","Please wait...",false,false);  // for  showing the 
    // dialog  where context is the current context, next field is title followed by
    // message to be shown to the user and in the end intermediate field
    loadingDialog.dismiss();// for dismissing the dialog 

for more info Android - What is difference between progressDialog.show() and ProgressDialog.show()?

Caliban answered 11/3, 2017 at 8:20 Comment(0)
V
1

ProgressDialog is deprecated since API 26

still you can use this:

public void button_click(View view)
{
    final ProgressDialog progressDialog = ProgressDialog.show(Login.this,"Please Wait","Processing...",true);
}
Viens answered 11/1, 2018 at 8:7 Comment(0)
L
1

Simple Way :

ProgressDialog pDialog = new ProgressDialog(MainActivity.this); //Your Activity.this
pDialog.setMessage("Loading...!");
pDialog.setCancelable(false);
pDialog.show();
Lemieux answered 8/5, 2018 at 5:26 Comment(0)
T
0
        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();
Tetroxide answered 4/5, 2012 at 9:42 Comment(0)
Y
0

Whenever you want ProgressDialog call this method

    private void startLoader() {
    progress = new ProgressDialog(this);    //ProgressDialog
    progress.setTitle("Loading");
    progress.setMessage("Please wait");
    progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progress.setCancelable(false);
    progress.show();
    new Thread(new Runnable() {
        public void run() {
            try {
                Thread.sleep(7000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            progress.dismiss();
        }
    }).start();
}
Ytterbium answered 28/7, 2021 at 9:25 Comment(0)
S
0

Create your own ProgressDialog in Java:

Note: Used with Android 33.

private AlertDialog progressAlertDialog = createProgressDialog(currentActivity / this);

private AlertDialog createProgressDialog(AppCompatActivity currentActivity) {
    LinearLayout vLayout = new LinearLayout(currentActivity);
    vLayout.setOrientation(LinearLayout.VERTICAL);
    vLayout.setPadding(50, 50, 50, 50);
    vLayout.addView(new ProgressBar(currentActivity, null, android.R.attr.progressBarStyleLarge));

    return new AlertDialog.Builder(currentActivity)
            .setCancelable(false)
            .setView(vLayout)
            .create();
}

public void displayProgressDialog() {
    if (!progressAlertDialog.isShowing()) {
        progressAlertDialog.show();
    }
}

public void hideProgressDialog() {
    progressAlertDialog.dismiss();
}
Strainer answered 8/10, 2023 at 15:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.