How to show progress dialog in Android?
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
Show ProgressDialog Android
Declare your progress dialog:
ProgressDialog progress;
When you're ready to start the progress dialog:
progress = ProgressDialog.show(this, "dialog title",
"dialog message", true);
and to make it go away when you're done:
progress.dismiss();
Here's a little thread example for you:
// Note: declare ProgressDialog progress as a field in your class.
progress = ProgressDialog.show(this, "dialog title",
"dialog message", true);
new Thread(new Runnable() {
@Override
public void run()
{
// do the thing that takes a long time
runOnUiThread(new Runnable() {
@Override
public void run()
{
progress.dismiss();
}
});
}
}).start();
Android - how to show progress dialog while service is running?
create broadcast messages when you want to show or hide progress bar from your service:
Intent i = new Intent("yourPackage.SHOW_Progress");
sendBroadcast(i);
then create broadcast receiver and handle received messages:
public class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
MainActivity mainActivity = ((MyApplication) context.getApplicationContext()).mainActivity;
mainActivity.showProgress();
}
}
and inside your mainActivity create method for show or hide progress bar
How to display a ProgressDialog and dismiss it when finished doing a job
Move pd.dismiss()
to the bottom of async block.
But don't forget to run it on UI thread activity.runOnUiThread { pd.dismiss() }
Related Topics
Android Studio Create Project Template for New Projects
How to Attach Android Source to Eclipse
Android - Add Textview to Layout When Button Is Pressed
How to Get the Selected Item from Listview
Android - Gettargetfragment and Settargetfragment - What Are They Used For
Android: Backspace in Webview/Baseinputconnection
Firebase Realtime Database Connection Killed: Different Region
Android Device Does Not Show Up in Adb List
Android Xml Rounded Clipped Corners
Android: Using Objectanimator to Translate a View with Fractional Values of the View's Dimension
Disable the Touch Events for All the Views
Is Google's Android Opengl Tutorial Teaching Incorrect Linear Algebra