Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to make Button text flash/blink?

I have a question similar to this but I am wanting to make only the text on the button flash. I don't want the button background to also flash.

This is my R.anim.blink.xml file:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha android:fromAlpha="0.0"
           android:toAlpha="1.0"
           android:interpolator="@android:anim/accelerate_interpolator"
           android:duration="500"
           android:startOffset="20"
           android:repeatMode="reverse"
           android:repeatCount="infinite"/>
</set>

But this code...

Animation blinkingAnimation = AnimationUtils.loadAnimation(this, R.anim.blink);
myButton.setAnimation(blinkingAnimation);

...makes the whole button blink. So how to make just the text blink (so the button background is shown all the time)?

like image 245
ban-geoengineering Avatar asked Oct 17 '22 07:10

ban-geoengineering


2 Answers

Simple way:

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

    final Button btn = (Button) findViewById(R.id.btn);
    final ObjectAnimator colorAnim = ObjectAnimator.ofInt(btn, "textColor", Color.BLACK, Color.TRANSPARENT); //you can change colors
    colorAnim.setDuration(500); //duration of flash
    colorAnim.setEvaluator(new ArgbEvaluator());
    colorAnim.setRepeatCount(ValueAnimator.INFINITE);
    colorAnim.setRepeatMode(ValueAnimator.REVERSE);
    colorAnim.start();


    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View view) {
            colorAnim.end();
            colorAnim.cancel();
        }
    });
}

It will finish flashing after pressing.

EDIT:

You can define your animation in xml (use objectAnimator):

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="textColor"
    android:duration="500"
    android:valueFrom="#000000"
    android:valueTo="@android:color/transparent"
    android:repeatMode="reverse"
    android:repeatCount="infinite"
    android:interpolator="@android:anim/accelerate_interpolator" />

and use it in your code:

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


    final ObjectAnimator animator = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.blink);
    final Button btn = (Button) findViewById(R.id.btn);
    animator.setTarget(btn);
    animator.start();

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View view) {
            animator.end();
            animator.cancel();
        }
    });
}

XML must be in the 'animator' folder.

like image 102
antonid Avatar answered Oct 21 '22 04:10

antonid


Try this code in oncreate method of Activity

    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();
            //also your extra work here
        }
    });
like image 33
Bapusaheb Shinde Avatar answered Oct 21 '22 05:10

Bapusaheb Shinde