1

So, I have a program which, when pressing a button, changes an image src programmatically (with a method I made) then calls another method I made, but I want for the second method to be called after a delay of a few seconds. I already tried Thread.sleep(5000), java.util.concurrent.TimeUnit.SECONDS.sleep(2), and wait() but all of these pauses the whole program hindering the change of the image src.

Here is the code :

public void onButtonClickListenerGoHit(int buttonId) {
    button_test = findViewById(buttonId);
    button_test.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    changeImage(R.id.imageView2, R.drawable.lodef);
                    gameOver();
                }
            }
    );
}

So what I want is for the gameOver() to be called a few seconds after changeImage().

  • Does this answer your question? [How to run a method every X seconds](https://stackoverflow.com/questions/11434056/how-to-run-a-method-every-x-seconds) – Kenneth Clark May 07 '20 at 13:50

1 Answers1

0

Here is a Kotlin example

Handler().postDelayed({
  // call your method
}, 1000 /*delay in milliseconds*/)

java

new Handler().postDelayed(new Runnable() {
 @Override
 public void run () {
   // make operation on UI - on example
   // on progress bar.
 }
}, 1000);
Dmytro Batyuk
  • 957
  • 8
  • 15