When I'm clicking on a button, I want to change an Image view to different picture, to wait 3 seconds, and to change it again to another picture (without clicking again).
1 click -> change picture -> wait 3 seconds -> change picture.
This is my code:
northLight.setImageResource(R.drawable.red_and_yellow);
try {
TimeUnit.MILLISECONDS.sleep(3000);
} catch (InterruptedException e) {}
northLight.setImageResource(R.drawable.green);
While I'm running the program, when I'm actually clicking the button, the program ignores the first setImage
and changes it straight to the second setImage
(to the green).
How can I solve this?
You can try using a Handler to wait and change the image. When your button gets clicked, change your image and run the handler with a delay of 3 seconds.
//Call this method when your button is clicked
public void changeImage() {
northLight.setImageResource(R.drawable.red_and_yellow);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
northLight.setImageResource(R.drawable.green);
}
}, 3000);
}