cjhulse
cjhulse

Reputation: 23

How do I make sure an animation is allowed to complete before the next instruction is executed?

I'm new to Android development, I have created an animation which I have associated with a button onClick action however the animation doesn't seem to complete before the next command is executed. I know the animation works because if I comment out the openWallet() command I can watch it run as intended. Any ideas how I can make sure the animation completes before openWallet() is executed? openWallet() loads a different xml layout so I suspect the animation might be going on in the background?

private Button useButton;
private Animation buttonPulseAnimation = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.front_portrait);
    useButton = (Button) findViewById(R.id.icon_image);
    buttonPulseAnimation = AnimationUtils.loadAnimation(this, R.anim.button_pulse);
    useButton.startAnimation(buttonPulseAnimation);
    useButton.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            useButton.startAnimation(buttonPulseAnimation);
            openWallet();
        }
    });

Upvotes: 2

Views: 215

Answers (1)

Vineet Shukla
Vineet Shukla

Reputation: 24031

set animation listener and onAnimationEnd do your next task:

buttonPulseAnimation.setAnimationListener(new Animation.AnimationListener() {

        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub

        }

        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

        public void onAnimationEnd(Animation animation) {
            // Define your next task here.....

        }
    });

Upvotes: 1

Related Questions