How can I programmatically display an hourglass in an Android application?
Asked
Active
Viewed 2.0k times
2 Answers
45
You can use a ProgressDialog
:
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Thinking...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
The above code will show the following dialog on top of your Activity
:
Alternatively (or additionally) you can display a Progress indicator in the title bar of your Activity
.
You need to request this as a feature near the top of the onCreate()
method of your Activity
using the following code:
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
Then turn it on like this:
setProgressBarIndeterminateVisibility(true);
and turn it off like this:
setProgressBarIndeterminateVisibility(false);

Community
- 1
- 1

David Webb
- 190,537
- 57
- 313
- 299
-
The problem is that after displaying the dialog box I ran a relatively long treatment which prevents the display of the dialog box that appears at the end of treatment when I no longer need ! – Arutha Jan 26 '10 at 16:00
-
1Have a look at `AsyncTask`. You display and hide the `ProgressDialog` in `onPreExecute()` and `onPostExecute` and do your work in `doInBackground`. http://android-developers.blogspot.com/2009/05/painless-threading.html – David Webb Jan 26 '10 at 16:26
-
Might also be worth reading the Android Developer Guide "Designing For Responsiveness" http://developer.android.com/guide/practices/design/responsiveness.html – Jeremy Logan Jan 26 '10 at 17:02
-
The constructor ProgressDialog() is undefined . How to solve or what to replaced the "this" ? – neobie Oct 13 '11 at 08:22
-
@neobie Set "this" to the context that you want to display the Progress Dialog to. – Ushal Naidoo Mar 11 '13 at 21:31
-
every Android post older than 2 years should be wiped – Atys Nov 15 '21 at 19:52
3
Here is a simple example of doing it using AsyncTask:
public class MyActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
...
new MyLoadTask(this).execute(); //If you have parameters you can pass them inside execute method
}
private class MyLoadTask extends AsyncTask <Object,Void,String>{
private ProgressDialog dialog;
public MyLoadTask(MyActivity act) {
dialog = new ProgressDialog(act);
}
protected void onPreExecute() {
dialog.setMessage("Loading...");
dialog.show();
}
@Override
protected String doInBackground(Object... params) {
//Perform your task here....
//Return value ... you can return any Object, I used String in this case
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return(new String("test"));
}
@Override
protected void onPostExecute(String str) {
//Update your UI here.... Get value from doInBackground ....
if (dialog.isShowing()) {
dialog.dismiss();
}
}
}

live-love
- 48,840
- 22
- 240
- 204