How to disable button while AlphaAnimation running

Tags: , ,



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.

Answer

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.



Source: stackoverflow