14

I would like to know if it's possible to use handler().postdelayed twice?

I mean, I want to create a button, that when clicked it change the color and stay in this state 1 second, then, after 1 second another button change the color.

I've created the following code:

In the onclicklistener:

btn3.setBackgroundColor(Color.WHITE);
 new Handler().postDelayed(new Runnable() {
 @Override
 public void run() {
 checkAnswer();
 waitAnswer();
 btnRsp3.setBackgroundResource(R.drawable.selector); 
 }
 }, 1000);

CheckAnswer:

 public void CheckAnswer(){
 btn1.setBackgroundColor(Color.GREEN);
 new Handler().postDelayed(new Runnable() {
 @Override
 public void run() {
 }
}, 500);
btn1.setBackgroundResource(R.drawable.selector);
}

I think the problem is on CheckAnswer because it seems it doesn't stop in this postDelayed and step to the waitAnswer.

Thanks

asked Aug 17, 2013 at 2:29

2 Answers 2

21

Why do you expect it to stop on postDelayed? postDelayed places your Runnable to the Handler Looper queue and returns. Since both handlers are created on the same looper, the second runnable is executed after the first one terminates (plus whatever left of the 500 ms delay)

UPDATE:

You need something like that

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
 @Override
 public void run() {
 btn1.setBackgroundColor(Color.GREEN);
 }
}, 1000);
handler.postDelayed(new Runnable() {
 @Override
 public void run() {
 btn1.setBackgroundResource(R.drawable.selector);
 }
}, 2000);
answered Aug 17, 2013 at 2:38
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for you answer, but I don't understand: if the second runnable is executed after the first one terminates why it doesn't change the btn to green color like i code in checkAnswer class? It skips this step no?
your second Runnable is empty. checkAnswer changes color to green, then queues empty rectangle, then sets background resource, so you won't see it green
Yes, but I thought if I set color to green and create a queue empty of 500 ms, then set background again, i see green 500 ms.
well, I'm not sure what would "a queue empty of 500 ms" mean, but anyway it works as I described in my answer.
Let me understand clearly something. If you have for example the following code: A; Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { B; } }, 1000); C; It will execute operation A, then wait 1 second and execute operation B and after that execute operation C right?
|
1
new Handler().postDelayed(new Runnable() 
{
 @Override
 public void run() 
 {
 //Your Work
 }
 }, 1000);
answered Jul 3, 2019 at 11:00

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.