-1

I was making an activity with a lot of buttons.

I want to make the buttons flash when the other button is clicked.

For example, there is four buttons. (A, B, C, D)

When button A is clicked, Button B changes its color for 100 ms and revert.

And after button B revert its color, button C does it again, and button D also.

I found how to make delay, and stuck with this.

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
    //Do something after 100ms
  }
}, 100);

How to call a method after a delay in Android

This was the Question.

What method should I use for this?

Community
  • 1
  • 1
ntcho
  • 301
  • 4
  • 13

1 Answers1

1

Here's fishing pole (not a fish) -> what you need to do in your runnable is to setBackground() or setBackgroundResource() (mind the API version!) on the button you want and then fire another runnable with delay). The second runnable should again do setBackground()/setBackgroundResource() and restore what was previously. Of course you can do that with one runnable (running itself) and couple of conditions too.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • Yes. you fire the code from your question because you need the job done with delay AND on UI thread. Of course you need to add some code to actually do the change but this is the right way of firing delayed tasks like this one – Marcin Orlowski Aug 03 '15 at 10:30
  • So I need to use that code 6 times, right? Changing the first one and reverting, and the second one and third one. – ntcho Aug 03 '15 at 10:34
  • Not really. You at worst need 2 runnables (one to set new background and other to restore) and at best 1 runnable with `if()`s). Number of buttons does not matter because you can extend `Runnable` and pass Button in the constructor – Marcin Orlowski Aug 03 '15 at 10:36
  • Could you make an example.. I have no idea how to put `setBackground()` in that code. – ntcho Aug 03 '15 at 10:41