How do you display a progress dialog before starting an activity (i.e., while the activity is loading some data) in Android?
Asked
Active
Viewed 3.5k times
36

Swati Garg
- 995
- 1
- 10
- 21

Neha
- 681
- 2
- 9
- 15
-
you should remove the spaces you have before your question... It is getting picked up as a code block and making your question harder to read. – dting Mar 05 '11 at 06:35
2 Answers
65
You should load data in an AsyncTask and update your interface when the data finishes loading.
You could even start a new activity in your AsyncTask's onPostExecute()
method.
More specifically, you will need a new class that extends AsyncTask:
public class MyTask extends AsyncTask<Void, Void, Void> {
public MyTask(ProgressDialog progress) {
this.progress = progress;
}
public void onPreExecute() {
progress.show();
}
public void doInBackground(Void... unused) {
... do your loading here ...
}
public void onPostExecute(Void unused) {
progress.dismiss();
}
}
Then in your activity you would do:
ProgressDialog progress = new ProgressDialog(this);
progress.setMessage("Loading...");
new MyTask(progress).execute();

Artem Russakovskii
- 21,516
- 18
- 92
- 115

Matthew
- 44,826
- 10
- 98
- 87
-
Hey thanks for the answer, I'm doing exactly this, but for some reason the progressDialog pops up when the phone is in landscape orientation but not when it's in portrait orientation. Do you have any ideas on this? I've tried starting the dialog from the first class (the one that calls myTask) but still the same problem. Sometimes, the dialog works in portrait mode but it looks out of focus and this happens only once in 10 tries. – Art Apr 21 '13 at 04:59
3
When you start a long-running process on Android, its always advisable to do it on another thread. You can then use the UI thread to display a progress dialog. You cannot display a progress dialog in the same (UI) thread in which the process is running.
Do the following to start your process
pd = ProgressDialog.show(this, "Synchronizing data", "Please wait...");
Thread t = new Thread(this);
t.start();
For this your activity should implement Runnable as follows
public class SyncDataActivity extends Activity implements Runnable
And finally a method to perform the long-running process
@Override
public void run() {
//your code here
}

Arun
- 3,036
- 3
- 35
- 57
-
If you use threads you have to manage various things such as executing post-task code on the UI thread. – Matthew Mar 05 '11 at 06:50
-
-
Hi,I tried as you guys told, but still its not working. I am posting my code below. can anyone please tell that what i am doing wrong? – Neha Mar 05 '11 at 07:32
-
public class Startup extends Activity implements Runnable{ private ProgressDialog pd @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.startup); new MyTask(pd).execute(); } } – Neha Mar 05 '11 at 07:36
-
private class MyTask extends AsyncTask
{ private ProgressDialog progress; public MyTask(ProgressDialog progress) { this.progress = progress; } protected void onPreExecute() { progress = ProgressDialog.show(Startup.this, "", "Loading. Please wait...", true); } @Override protected Void doInBackground(Void... arg0) { //Excecuting some methods return null; } @Override protected void onPostExecute(Void result) { progress.dismiss(); }} – Neha Mar 05 '11 at 07:36 -
@Neha.. It is not clear to me why your Activity implements Runnable. Consider placing the method that gets the data into doInBackground. Consider returning the data from the getData method. This data will be automagically passed to onPostExecute. Update the UI with the data in onPostExecute. If the UI update takes time, then you have a bigger problem, but I believe that that problem can be solved using a handler loop. – JAL Mar 05 '11 at 20:05
-
@Jal: Thanks a lot for your suggestion :) By the way, Matthew's solution worked for me. – Neha Mar 07 '11 at 13:16