jobin
jobin

Reputation: 1527

animations unresponsive inside button click

I am trying to animate the button on click.It worked only once .The animation I used was Alpha.It works outside of button click.Can anyone find the reason for this strange behaviour?

Code:

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
AlphaAnimation alphaAnimation=new AlphaAnimation(1.0f,0.0f);

                alphaAnimation.setDuration(2000);
                button.setAnimation(alphaAnimation);
                alphaAnimation.start();
                alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
               Log.e("Alpha","a");}
});

Upvotes: 0

Views: 43

Answers (1)

webianks
webianks

Reputation: 134

Use startAnimation.

From official documentation:

void setAnimation (Animation animation)

Sets the next animation to play for this view. If you want the animation to play immediately, use startAnimation(android.view.animation.Animation) instead. This method provides allows fine-grained control over the start time and invalidation, but you must make sure that 1) the animation has a start time set, and 2) the view's parent (which controls animations on its children) will be invalidated when the animation is supposed to start.

Below is a sample snippet.

 AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.0f);
    alphaAnimation.setDuration(2000);
    alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {

        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
    button.startAnimation(alphaAnimation);

Upvotes: 1

Related Questions