title: Running animation after animation, android studio
body: <p>I am new at android programming and I have a problem to run animation after animation in android studio. The thing is that I don't have specified number of animations I need, it depends on counter. </p>
  
  <pre><code>package com.example.smartpc.memorygame;
  
  import android.graphics.drawable.TransitionDrawable;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.view.animation.Animation;
  import android.view.animation.AnimationSet;
  import android.view.animation.RotateAnimation;
  import android.widget.Button;
  import android.widget.ImageView;
  
  import java.util.ArrayList;
  import java.util.Random;
  
  public class MemoryGame extends AppCompatActivity {
  
      ImageView imgRed;
      ImageView imgGreen;
      ImageView imgBlue;
      Button btnStart;
  
      ArrayList&lt;Integer&gt;array=new ArrayList&lt;&gt;();
      int counter=3;
      int i=0;
      Boolean flag=false;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_memory_game);
  
          imgRed=(ImageView)findViewById(R.id.imgRed);
          imgGreen=(ImageView)findViewById(R.id.imgGreen);
          imgBlue=(ImageView)findViewById(R.id.imgBlue);
          btnStart=(Button)findViewById(R.id.btnStart);
  
          final RotateAnimation animation=new RotateAnimation(0f,360f, RotateAnimation.RELATIVE_TO_SELF,0.5f,
                                                              RotateAnimation.RELATIVE_TO_SELF,0.5f);
          animation.setDuration(1000);
  //        animation.setRepeatCount(1);
  
          animation.setAnimationListener(new Animation.AnimationListener() {
              @Override
              public void onAnimationStart(Animation animation) {
                  ;
              }
  
              @Override
              public void onAnimationEnd(Animation animation) {
                  if (flag==true) {
                      i++;
                      Random rand = new Random();
                      int number = Math.abs(rand.nextInt() % 3);
                      btnStart.setText(Integer.toString(i) + " " + Integer.toString(number));
                      if (number == 0) {
                          imgRed.startAnimation(animation);
                      } else if (number == 1) {
                          imgGreen.startAnimation(animation);
                      } else if (number==2){
                          imgBlue.startAnimation(animation);
                      }
                      if (i == counter - 1) {
                          i = 0;
                          flag=false;
                          return;
                      }
                  }
              }
  
              @Override
              public void onAnimationRepeat(Animation animation) {
                  ;
              }
          });
  
          imgRed.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  imgRed.startAnimation(animation);
                  flag=false;
              }
          });
  
          imgGreen.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  imgGreen.startAnimation(animation);
                  flag=false;
              }
          });
  
          imgBlue.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  imgBlue.startAnimation(animation);
                  flag=false;
              }
          });
  
          btnStart.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  //              imgGreen.setImageResource(R.drawable.transition);
  //              ((TransitionDrawable) imgGreen.getDrawable()).startTransition(3000);
                  flag=true;
                  Random rand = new Random();
                  int number = Math.abs(rand.nextInt() % 3);
  
                  if (number == 0) {
                      imgRed.startAnimation(animation);
                  }
                  else if (number == 1) {
                      imgGreen.startAnimation(animation);
                  }
                  else if (number==2) {
                      imgBlue.startAnimation(animation);
                  }
  //                btnStart.setText(Integer.toString(i) + " " + Integer.toString(number));
              }
          });
      }
  }
  </code></pre>
  
  <p>In method onAnimationEnd() is problem. It generates number and starts good animaton, but it also starts another one (random, there's no rule which one). Does anyone have idea how to solve this? Also, counter is not fixed, I want to change it in code later.</p>