Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android hourglass

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

like image 421
Arutha Avatar asked Jan 26 '10 14:01

Arutha


2 Answers

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:

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:

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

Then turn it on like this:

setProgressBarIndeterminateVisibility(true);

and turn it off like this:

setProgressBarIndeterminateVisibility(false);
like image 181
Dave Webb Avatar answered Oct 17 '22 02:10

Dave Webb


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();
            }           
        }
    }
like image 36
live-love Avatar answered Oct 17 '22 00:10

live-love