0

I have two functions that animate two different TextViews but they both seem to start at the same time but I am trying to have the animation of the first function finish first and then start the second function.

public Boolean functionFinished = false;


public void runFunction(){
    firstFunction();
    if(functionFinished = true){
       secondFunction();
    }

}


public void firstFunction(){

        initialCount = (TextView) findViewById(R.id.textView_Fade);

        initialCount.setText("3");
        final Animation out = new AlphaAnimation(1.0f, 0.0f);
        out.setDuration(1000);
        initialCount.startAnimation(out);

        out.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if(initialCount.getText().equals("3")){
                    initialCount.setText("2");
                    initialCount.startAnimation(out);
                } else if(initialCount.getText().equals("2")){
                    initialCount.setText("1");
                    initialCount.startAnimation(out);
                } else if (initialCount.getText().equals("1")){
                    initialCount.setText("START!");

                }

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
   functionFinished = true;
}

The second function simply changes its own textView every second counting up from 0.

What did I do wrong / how do I correct this so that the second function runs after the first function has finished? (ie. sets functionFinished to true only when the TextView from the firstFunction is "START!")

Kadana Kanz
  • 177
  • 2
  • 11

3 Answers3

3
public void runFunction() {
    firstFunction();
}

public void firstFunction() {
    initialCount = (TextView) findViewById(R.id.textView_Fade);

    initialCount.setText("3");
    final Animation out = new AlphaAnimation(1.0f, 0.0f);
    out.setDuration(1000);

    out.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            ...
            secondFunction();
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }
    });

    initialCount.startAnimation(out);
}

Just call second function from onAnimationEnd. Also keep in mind that you should attach listener before start animation.

Divers
  • 9,531
  • 7
  • 45
  • 88
  • @Divers! I have a similar question, can you help me? Because I have tried with AnimationListener and it doesn't work! My question is http://stackoverflow.com/questions/33987892/how-to-add-delay-between-animations – LordCommanDev Dec 01 '15 at 19:04
  • 1
    Thanks so much for sharing. You helped me a lot @Divers :) – Tomas M Mar 16 '21 at 15:33
1

If your minimum sdk version is 14:-

textView1.animate()
         .alpha(0f)
         .setDuration(400)
         .setAnimationListener(new AnimationListener() { 
          @Override 
          public void onAnimationEnd(Animation arg0) {
               // animate second textview here....
          } 
}); 
Paresh P.
  • 6,677
  • 1
  • 14
  • 26
0

Because you assign functionFinished = true at the end of the firstFunction().

And you must misunderstand that initialCount.startAnimation(out) will block the code and firstFunction() completes only after animation ends .

initialCount.startAnimation(out) will not block any code ,and functionFinished = true is run immediately. so secondFunction() will run immediately after firstFunction() finishes instead of animation finishes.

Zhli
  • 380
  • 1
  • 10