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

loading27
  • 153
  • 1
  • 1
  • 5

2 Answers2

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);
msh
  • 2,700
  • 1
  • 17
  • 23
  • 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? – loading27 Aug 17 '13 at 02:43
  • 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 – msh Aug 17 '13 at 02:51
  • 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. – loading27 Aug 17 '13 at 02:57
  • well, I'm not sure what would "a queue empty of 500 ms" mean, but anyway it works as I described in my answer. – msh Aug 17 '13 at 03:02
  • 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? – loading27 Aug 17 '13 at 15:58
  • @loading27 In that scenario, the order of execution will be A, C, <1000ms - (execution time of C)>, B. The reason was explained by @msh: when the `postDelayed(Runnable, long)` method is called, it defines an operation to run 1000ms from now and **returns immediately**. The result is that C is executed immediately after `postDelayed()` returns, and assuming C executes in under 1000ms, B will execute last. – Jin Jan 28 '16 at 19:40
1
new Handler().postDelayed(new Runnable() 
{
        @Override
        public void run() 
        {
            //Your Work
        }
  }, 1000);
Keshav Gera
  • 10,807
  • 1
  • 75
  • 53