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();
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() }
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 show ProgressDialog across launching a new Activity?
You can show a progress dialog like this -
Define this
private ProgressDialog pd = null;
in your activity class
Put this in your onCreate (Dont setContentView directly here)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.pd = ProgressDialog.show(this, "Fancy App",
"Loading...Please wait...", true, false);
// Start a new thread that will download all the data
new IAmABackgroundTask().execute();
}
// Background heavy lifting
class IAmABackgroundTask extends
AsyncTask<String, Integer, Boolean> {
@Override
protected void onPreExecute() {
// showDialog(AUTHORIZING_DIALOG);
}
@Override
protected void onPostExecute(Boolean result) {
// Pass the result data back to the main activity
ActivityName.this.data = result;
if (ActivityName.this.pd != null) {
ActivityName.this.pd.dismiss();
}
setContentView(R.layout.main);
}
@Override
protected Boolean doInBackground(String... params) {
//Do all your slow tasks here but dont set anything on UI
//ALL ui activities on the main thread
return true;
}
}
Also go through this :http://developer.android.com/training/improving-layouts/index.html for optimizing layout performance.
Also Use Traceview to look for bottlenecks
Related Topics
What Does Porterduff.Mode Mean in Android Graphics.What Does It Do
Lock Screen Orientation (Android)
How to Create Text File and Insert Data to That File on Android
Unexpected Value from Nativegetenabledtags: 0
Transfer Data Between iOS and Android via Bluetooth
Selecting Multiple Items in Listview
Copy Database from Assets Folder in Unrooted Device
Notifydatasetchange Not Working from Custom Adapter
How to Apply a Style to All Buttons of an Android Application
Changing the Android Overflow Menu Icon Programmatically
Android Dialogfragment VS Dialog
Sqliteopenhelper Problem with Fully Qualified Db Path Name
How to Sign an Already Compiled APK
Android Webview Not Loading an Https Url
Android: How to Make a Clickable Map Image with Each Country Producing a Different Action
Alphabetindexer with Custom Adapter Managed by Loadermanager
Ffmpeg for a Android (Using Tutorial: "Ffmpeg and Android.Mk")