Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable button while AlphaAnimation running

I want to disable click on button when the animation running. the code is below:

    AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
    anim.setDuration(4000);
    anim.setRepeatMode(Animation.REVERSE);
    btnTag.startAnimation(anim);

So i want to cant click the button until animation done.

like image 203
Muhammad Dyas Yaskur Avatar asked Mar 14 '26 20:03

Muhammad Dyas Yaskur


1 Answers

I normally accomplish something like this is using an AnimationListener. It allows you to run code at various stages of the animation.

This code is untested, but the way it should look is:

AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(4000);
anim.setRepeatMode(Animation.REVERSE);
anim.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {
        btnTag.setClickable(false);
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        btnTag.setClickable(true);
    }

    @Override
    public void onAnimationRepeat(Animation animation) {}
});
btnTag.startAnimation(anim);

Not sure if btnTag is your button or a view holding your button, but call the button's setClickable(boolean clickable) method to enable and disable the button.

like image 53
GleasonK Avatar answered Mar 16 '26 10:03

GleasonK