How to make a Button blink in Android?

android, performance, runnable

Solution

You can animate your button once you set its color to say GREEN. I mean,

if(answerTrue){

    // Set the color of the button to GREEN once.

    // Next, animate its visibility with the set color - which is GREEN as follows:

    Animation anim = new AlphaAnimation(0.0f, 1.0f);
    anim.setDuration(50); //You can manage the blinking time with this parameter
    anim.setStartOffset(20);
    anim.setRepeatMode(Animation.REVERSE);
    anim.setRepeatCount(Animation.INFINITE);
    button.startAnimation(anim);
}

Similarly, you can animate the other button and stop animation when you feel like.

Source: Blinking Text in android view

Problem

If the user (in my quizgame) chooses the false answer, the button with the correct answer should blink green. So far i did it like this: ``` if(answerTrue) for (int i = 0; i < 2000; i = i + 250) { handler.postDelayed(rbl_blinkNormal, i); i = i + 250; handler.postDelayed(rbl_blinkGreen, i); } ``` And the runnables: Green: ``` rbl_blinkGreen= new Runnable() { @Override public void run() { btn_richtig.setBackgroundResource(R.drawable.color_green_btn); } }; ``` Normal: ``` rbl_blinkNormal= new Runnable() { @Override public void run() { btn_richtig.setBackgroundResource(R.drawable.color_black_btn); } }; ``` It works fine but like this Im calling the postDelayed() every 250ms. May this impact my application performance and is there any better way to do this?

Original source

Related problems