I am working on an Android project and I need to have the program wait for 5 seconds to display a message and then move on. I have looked around and I tried the Thread sleep(mili); but it didn’t work so… I included my code and am I just doing something wrong or what else can I do?.
...
text.setText("You picked a goat");
lose++;
loser.setText("You have picked a goat " + lose + " time(s)");
Thread time = new Thread()
{
@Override
public void run(){
try {
synchronized(this){
wait(5000);
}
}
catch(InterruptedException ex){}
}
};
time.start();
…
I also tried putting this in its own method and having it be called. The lose is just a counter and the “text” and “loser” are just textviews
One way to do this is to use the Handler class, which will allow you to call a
Runnableafter a given number of milliseconds. This is useful for short timespans, like in your case.For example, using the postDelayed() method: