title: Animation Drawable + Android + Start and stop animation
body: <p>I am creating an application that implements an animation, when a certain variable is true, when I start the activity and the variable is true, the animation works, but if I change the variable, and then change it back to the true variable the animation will not work. Does anybody have any ideas as to why?</p>
  
  <p>Declare Variables</p>
  
  <pre><code>// Load the ImageView that will host the animation and
           // set its background to our AnimationDrawable XML resource.
           chargeStatus = (ImageView)findViewById(R.id.chargeStatus);
          chargeStatus.setBackgroundResource(R.drawable.chargeon);
  
           // Get the background, which has been compiled to an AnimationDrawable object.
           chargeAnimation = (AnimationDrawable) chargeStatus.getBackground();   
  </code></pre>
  
  <p>Starts the thread on app start </p>
  
  <pre><code> /////When Application Starts
  @Override
  public void onStart() 
  {
      super.onStart();
  
      Thread cThread = new Thread(new serverThread()); //Starts the thread that initiates     communications
      cThread.start();
  </code></pre>
  
  <p>Thread That sets the value</p>
  
  <pre><code>public class serverThread implements Runnable 
  {
      //public boolean connected = false; //connection
      public void run() //runs the thread
      {
  
          try 
          {
              functions.connectToServer(serverIp); //connects to server
              connected = true;
  
              while (connected) 
              {
                  try 
                  {
                      connectStatus.setImageResource(R.drawable.blue_car);
                      functions.getStreams(); //establish stream connections
                      functions. updateStatus(); //updates the status of the server
                      changeButtonImage(); //sets initial state for charge button
  
  
  
                      try
                      {
                          Thread.sleep(2500);
                      }
                            catch ( InterruptedException interruptedException ) 
                            {
                               functions.displayMessage( "\nThread exception" );
                            } // end catch
  
                  }
                      catch (IOException e) 
                      {
                          Log.e("Client","Trouble Getting Streams",e);
  
  
                      }
  
  
  
              }//end while
          }//end try
              catch (Exception e)
              {
                  Log.e("Client", "Error Connecting", e);
                  connected = false;
                  connectStatus.setImageResource(R.drawable.red_car);
  
              }
      }; //end run
  }; //end serverThread     
  </code></pre>
  
  <p>Change button function</p>
  
  <pre><code>public void changeButtonImage(){
      runOnUiThread(new Runnable(){
  
  
          public void run() {
  
  
              if (functions.chargeStatNumber != 0) 
              {
                  // Start the charge animation 
                   chargeAnimation.start();
                  //set the Charge on off button to stop
                  chargeOnOff.setImageResource(R.drawable.charge_off);
  
              }
              else 
              {
                  // stop the charge animation if running
                   chargeAnimation.stop();
                  //set charge on off button to on
                  chargeOnOff.setImageResource(R.drawable.charge_on);
                  //set the charging symbol to off
                  chargeStatus.setImageResource(R.drawable.not_charging);
              }
          }});
  }
  </code></pre>
  
  <p>Thanks in advance for your help.</p>