Android - How to Make a Button Flash

How to make a Button blink in Android?

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

android - How can I make a button flash?

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();
}
});
}

How To make a small flash when a button is tapped?

Declare in drawables this selector and name it for example: button.xml

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true"
android:drawable="@drawable/btnPressed"/>
<item android:drawable="@drawable/btnNormal"></item>

</selector>

android:drawable can be color, image, another drawable...
And then you can declare your button as:

<Button 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button"
/>

If you create your buttons in code you can call method: setBackgroundResource() and pass resource id.
Example:

Button button = new Button(this);
button.setBackgroundResource(R.drawable.button);

http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

Android Studio : ImageButton image/background flash

I suggest you to use animation-list as background to your ImageButton.

1) Create a flash_background.xml file:

<?xml version="1.0" encoding="utf-8"?>

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">

<item android:drawable="@drawable/first_background" android:duration="200" />
<item android:drawable="@drawable/second_background" android:duration="200" />

</animation-list>

2) Then from code

yourButton.setBackgroundResource(R.drawable.flash_background);
AnimationDrawable anim = (AnimationDrawable) yourButton.getBackground();
anim.start();

Notice:

  • You can play with android:duration to speed up/down the animation.


Related Topics



Leave a reply



Submit