Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - How can I make a button flash?

Is there any way, in code, to make a button flash continually and then stop flashing when pressed?

like image 295
ron Avatar asked Jan 31 '11 14:01

ron


People also ask

How to make a button blink in Android?

Here is java code to animate android button ToggleButton btn =findViewById(R. id. img1);// identify your button here //this is the code to animate, place it under Onclick method so it will blink when button pressed Animation anim = new AlphaAnimation(0.5f, 1.0f); anim.


2 Answers

There are several, depending on what kind of flashing you mean. You can, for example, use alpha animation and start it as your button first appears. And when the user clicks button, in your OnClickListener just do clearAnimation().

Example:

public void onCreate(Bundle savedInstanceState) {
    final Animation animation = new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible
    animation.setDuration(500); // duration - half a second
    animation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
    animation.setRepeatCount(Animation.INFINITE); // Repeat animation infinitely
    animation.setRepeatMode(Animation.REVERSE); // Reverse animation at the end so the button will fade back in
    final Button btn = (Button) findViewById(R.id.your_btn);
    btn.startAnimation(animation);
    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(final View view) {
            view.clearAnimation();
        }
    });
}
like image 126
Alex Orlov Avatar answered Oct 16 '22 12:10

Alex Orlov


You can use this code and as well as you can also decide the blink timing of button via mAnimation.setDuration(200);.The code is as follows.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    select=(Button)findViewById(R.id.bSelect);
    Animation mAnimation = new AlphaAnimation(1, 0);
    mAnimation.setDuration(200);
    mAnimation.setInterpolator(new LinearInterpolator());
    mAnimation.setRepeatCount(Animation.INFINITE);
    mAnimation.setRepeatMode(Animation.REVERSE); 
    select.startAnimation(mAnimation);
    select.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            v.clearAnimation();


        }
    });

}
like image 13
Deepak Sharma Avatar answered Oct 16 '22 12:10

Deepak Sharma