Android hourglass-ThrowExceptions

Exception or error:

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

How to solve:

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:



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

Leave a Reply

Your email address will not be published. Required fields are marked *