1

I´ve got a quiz app where the user can answer some questions with radio buttons. Only one answer is correct.

When I click on my button to show the right answer, it is shown in a textView.

Until now I have another button to navigate to the next question.

b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
...

}

Now I want to change that. When the user press the button for showing the right question, I want the app 5 seconds to sleep and then navigate to the next question. How can I handle that?

user896692
  • 2,351
  • 7
  • 36
  • 57
  • The answer to this question might help you http://stackoverflow.com/questions/3342651/how-to-delay-or-sleep-a-java-program-in-few-second – Jackie Robinson Jan 22 '13 at 19:15

1 Answers1

2

I want the app 5 seconds to sleep and then navigate to the next question.

Use the Handler class. Simply call postDelayed() to execute the code you want in five seconds.


Every View already has a Handler so try this:

@Override
public void onClick(View v) {
    v.postDelayed(new Runnable() {
            @Override
            public void run() {
                // Do something
            }
        }, 5000);
}

This will execute the code inside run() 5 seconds after the Button has been clicked.

Sam
  • 86,580
  • 20
  • 181
  • 179