title: How to use AsyncTask inside of a for cycle in Android Java
body: <p>Please help me, I'm trying to create random button change colors using AsyncTask class inside of a for cycle (for a future game) but I don't know why it doesn't work properly, when the "RUN" button is pressed apparently the cycle is executed only once instead of 20, because the other buttons change color only once. here is my code: (thanks)</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:gravity="center_vertical"
  android:paddingBottom="@dimen/activity_vertical_margin"
  
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context="com.example.mybuttons.MainActivity" &gt;
  
      &lt;Button
          android:id="@+id/btnOne"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/btnRun"
          android:layout_alignParentTop="true"
          android:text="BUTTON ONE" /&gt;
  
      &lt;Button
          android:id="@+id/btnTwo"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/btnOne"
          android:layout_below="@+id/btnOne"
          android:text="BUTTON TWO" /&gt;
  
      &lt;Button
          android:id="@+id/btnThree"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/btnTwo"
          android:layout_below="@+id/btnTwo"
          android:text="BUTTON THREE" /&gt;
  
      &lt;Button
          android:id="@+id/btnFour"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/btnThree"
          android:layout_below="@+id/btnThree"
          android:text="BUTTON FOUR" /&gt;
  
      &lt;Button
          android:id="@+id/btnFive"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/btnFour"
          android:layout_below="@+id/btnFour"
          android:text="BUTTON FIVE" /&gt;
  
      &lt;Button
          android:id="@+id/btnRun"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_below="@+id/btnFive"
          android:layout_centerHorizontal="true"
          android:layout_marginTop="33dp"
          android:text="RUN" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>The code:</p>
  
  <pre><code>package com.myexercise;
  
  import java.util.Random;
  
  import android.app.Activity;
  import android.graphics.Color;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  
  public class MainActivity extends Activity implements OnClickListener {
  
      Button myOne;
      Button myTwo;
      Button myThree;
      Button myFour;
      Button myFive;
      Button myRun;
      int wichOne;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          myOne=(Button)findViewById(R.id.btnOne);
          myTwo=(Button)findViewById(R.id.btnTwo);
          myThree=(Button)findViewById(R.id.btnThree);
          myFour=(Button)findViewById(R.id.btnFour);
          myFive=(Button)findViewById(R.id.btnFive);
          myRun=(Button)findViewById(R.id.btnRun);
          myRun.setOnClickListener(this);
      }
      @Override
      public void onClick(View v) {
          for(int i=0;i&lt;20;i++){
              System.out.println("i="+i);
              MyAsyncTask runner = new MyAsyncTask(); 
              String sleepTime = "500";
              int myRandom=new Random().nextInt(5);
              String thisButton = Integer.toString(myRandom);
              runner.execute(sleepTime, thisButton);
          }
      }
      class MyAsyncTask extends AsyncTask&lt;String, String, String&gt; {
          private String resp;
          @Override
          protected String doInBackground(String... params) {
              try {
                  int time = Integer.parseInt(params[0]);  
                  wichOne = Integer.parseInt(params[1]);
                  Thread.sleep(time);
              } catch (InterruptedException e) {
                  e.printStackTrace();
                  resp = e.getMessage();
              } catch (Exception e) {
                  e.printStackTrace();
                  resp = e.getMessage();
              }
              return resp;
          }
          @Override
          protected void onPostExecute(String result) {
              myOne.setBackgroundColor(Color.LTGRAY);
              myTwo.setBackgroundColor(Color.LTGRAY);
              myThree.setBackgroundColor(Color.LTGRAY);
              myFour.setBackgroundColor(Color.LTGRAY);
              myFive.setBackgroundColor(Color.LTGRAY);
          }
          @Override
          protected void onPreExecute() {
              if(wichOne==0){
                  myOne.setBackgroundColor(Color.YELLOW);
              }else{
                  if(wichOne==1){
                      myTwo.setBackgroundColor(Color.BLUE);
                  } else {
                      if(wichOne==2){
                          myThree.setBackgroundColor(Color.CYAN);
                      } else{
                          if(wichOne==3){
                              myFour.setBackgroundColor(Color.RED);
                          } else{
                              myFive.setBackgroundColor(Color.DKGRAY);
                          }
                      }
                  }
              }
          }
      }
  }
  </code></pre>