Android hourglass


Solution 1

You can use a ProgressDialog:

ProgressDialog dialog = new ProgressDialog(this);

The above code will show the following dialog on top of your Activity:

alt text

Alternatively (or additionally) you can display a Progress indicator in the title bar of your Activity.

alt text

You need to request this as a feature near the top of the onCreate() method of your Activity using the following code:


Then turn it on like this:


and turn it off like this:


Solution 2

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

        protected String doInBackground(Object... params) {         
            //Perform your task here.... 
            //Return value ... you can return any Object, I used String in this case

            try {
            } catch (InterruptedException e) {
            return(new String("test"));

        protected void onPostExecute(String str) {          
            //Update your UI here.... Get value from doInBackground ....
            if (dialog.isShowing()) {

Related videos on Youtube

Author by


Updated on July 24, 2020


  • Arutha
    Arutha over 3 years

    How can I programmatically display an hourglass in an Android application?

  • Arutha
    Arutha almost 14 years
    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 !
  • David Webb
    David Webb almost 14 years
    Have a look at AsyncTask. You display and hide the ProgressDialog in onPreExecute() and onPostExecute and do your work in doInBackground.‌​tml
  • Jeremy Logan
    Jeremy Logan almost 14 years
    Might also be worth reading the Android Developer Guide "Designing For Responsiveness"…
  • neobie
    neobie about 12 years
    The constructor ProgressDialog() is undefined . How to solve or what to replaced the "this" ?
  • Ushal Naidoo
    Ushal Naidoo over 10 years
    @neobie Set "this" to the context that you want to display the Progress Dialog to.
  • Atys
    Atys about 2 years
    every Android post older than 2 years should be wiped