How to make an Android program 'wait'

OK, first of all, never implement a delay with a busy loop as you're doing. I can see where that comes from -- I'm guessing that the palm pilot was a single-process device with no built-in sleep() function, but on a multi-process device, a busy loop like that just brings the entire processor to its knees. It kills your battery, prevents normal system tasks from running properly, slows down other programs or stops them completely, makes the device unresponsive, and can even cause it to get hot in your hands.

The call you're looking for is Thread.sleep(). You'll need to set up to catch any interrupt exceptions that occur while you're sleeping.

Second, with event-based user interfaces such as Android (or pretty much any modern GUI system), you never want to sleep in the UI thread. That will also freeze up the entire device and result in an ANR (Activity Not Responding) crash, as other posters have mentioned. Most importantly, those little freezes totally ruin the user experience.

(Exception: if you're sleeping for short enough intervals that the user probably won't notice, you can get away with it. 1/4 second is probably ok, although it can make the application janky depending on the situation.)

Unfortunately, there's no clean and elegant way to do what you want if what you're doing is porting a loop-based application to an event-based system.

That said, the proper procedure is to create a handler in your UI thread and send delayed messages to it. The delayed messages will "wake up" your application and trigger it to perform whatever it was going to do after the delay.

Something like this:

View gameBoard;     // the view containing the main part of the game
int gameState = 0;  // starting
Handler myHandler;

public void onCreate(Bundle oldState) {
  super.onCreate(oldState);
  ...
  gameBoard = findViewById(R.layout.gameboard);
  myHandler = new Handler();
  ...
}

public void onResume() {
  super.onResume();
  displayStartingScreen();
  myHandler.postDelayed(new Runnable() { 
    gotoState1(); 
  }, 250);
}

private void gotoState1() {
  // It's now 1/4 second since onResume() was called
  displayNextScreen();
  myHandler.postDelayed(new Runnable() { 
    gotoState2();
  }, 250);
}
...

Do something like the following. Here is a link to the reference, this might be helpful.

final MyActivity myActivity = this;   

    thread=  new Thread(){
        @Override
        public void run(){
            try {
                synchronized(this){
                    wait(3000);
                }
            }
            catch(InterruptedException ex){                    
            }

            // TODO              
        }
    };

    thread.start();   

You really should not sleep the UI thread like this, you are likely to have your application force close with ActivityNotResponding exception if you do this.

If you want to delay some code from running for a certain amount of time use a Runnable and a Handler like this:

Runnable r = new Runnable() {
    @Override
    public void run(){
        doSomething(); //<-- put your code in here.
    }
};

Handler h = new Handler();
h.postDelayed(r, 1000); // <-- the "1000" is the delay time in miliseconds. 

This way your code still gets delayed, but you are not "freezing" the UI thread which would result in poor performance at best, and ANR force close at worst.