Currently, I am making an android app that is going to be a very simple memory game where 1 random button is going to be highlighted, then the user must click the button that was highlighted after the button goes back to normal. If the user gets the button correct the original button that was highlighted the first time will light up, then another random button will light up after just like the first time and they have to click them in order. For further clarification if your unsure its kind of like Simon (The game).

Currently the game is just going to the next random button instead of repeating AND THEN going to a new one and i'm unsure how to change that. Any help would be greatly appreciated!

```
package com.MakeItMobile.fixmymemory;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import android.R.drawable;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainScreen extends Activity implements OnClickListener {

Button buttonRed, buttonYellow, buttonOrange, buttonBlack, buttonGreen,
        buttonPurple, buttonPink, buttonLime, buttonDarkBlue;

Random randNumber;
List<Integer> whichButton;

int userInput[] = {};
int counter = 0;
int compareCounter = 0;
int n = 0;

Boolean yourTurn = false;
Boolean aiTurn = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.mainscreen);

    //Getting the buttons
    buttonRed = (Button) findViewById(R.id.buttonRed);
    buttonYellow = (Button) findViewById(R.id.buttonYellow);
    buttonOrange = (Button) findViewById(R.id.buttonOrange);
    buttonBlack = (Button) findViewById(R.id.buttonBlack);
    buttonGreen = (Button) findViewById(R.id.buttonGreen);
    buttonPurple = (Button) findViewById(R.id.buttonPurple);
    buttonPink = (Button) findViewById(R.id.buttonPink);
    buttonLime = (Button) findViewById(R.id.buttonLime);
    buttonDarkBlue = (Button) findViewById(R.id.buttonDarkBlue);

    //Setting them clickable
    buttonRed.setOnClickListener(this);
    buttonYellow.setOnClickListener(this);
    buttonOrange.setOnClickListener(this);
    buttonBlack.setOnClickListener(this);
    buttonGreen.setOnClickListener(this);
    buttonPurple.setOnClickListener(this);
    buttonPink.setOnClickListener(this);
    buttonLime.setOnClickListener(this);
    buttonDarkBlue.setOnClickListener(this);

    //Giving them a tag for easier comparison in onClick
    buttonRed.setTag(1);
    buttonYellow.setTag(2);
    buttonOrange.setTag(3);
    buttonBlack.setTag(4);
    buttonGreen.setTag(5);
    buttonPurple.setTag(6);
    buttonPink.setTag(7);
    buttonLime.setTag(8);
    buttonDarkBlue.setTag(9);

    //Showing a would you like to play dialog
    DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            switch (which) {
            case (DialogInterface.BUTTON_POSITIVE):
                whenStarted();
                break;

            case (DialogInterface.BUTTON_NEGATIVE):

                break;
            }
        }
    };
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Would you like to begin?")
            .setPositiveButton("Yes", dialogClickListener)
            .setNegativeButton("No", dialogClickListener).show();

}

// Main loop
public void whenStarted() {
    if (aiTurn) {
        whichButton = new ArrayList<Integer>();
        repeatBack();
        randomNumber();
        n = randomNumber();
        if (n == 1) {
            delay(R.drawable.buttonblueclicked, R.drawable.buttonblue);
        } else if (n == 2) {
            delay(R.drawable.buttonblueclicked, R.drawable.buttonblue);
        } else if (n == 3) {
            delay(R.drawable.buttonblueclicked, R.drawable.buttonblue);
        } else if (n == 4) {
            delay(R.drawable.buttonblueclicked, R.drawable.buttonblue);
        } else if (n == 5) {
            delay(R.drawable.buttonblueclicked, R.drawable.buttonblue);
        } else if (n == 6) {
            delay(R.drawable.buttonblueclicked, R.drawable.buttonblue);
        } else if (n == 7) {
            delay(R.drawable.buttonblueclicked, R.drawable.buttonblue);
        } else if (n == 8) {
            delay(R.drawable.buttonblueclicked, R.drawable.buttonblue);
        } else if (n == 9) {
            delay(R.drawable.buttonblueclicked, R.drawable.buttonblue);
        }
        whichButton.add(n);
        yourTurn = true;
    } else if (yourTurn) {

    }

}

// Repeating back what buttons were clicked each turn
public void repeatBack() {
    for (int i = 0; i < whichButton.size(); i++) {
        if (whichButton.get(i) == 1) {
            delayRepeat(R.drawable.buttonblueclicked, R.drawable.buttonblue);
        } else if (whichButton.get(i) == 2) {
            delayRepeat(R.drawable.buttonblueclicked, R.drawable.buttonblue);
        } else if (whichButton.get(i) == 3) {
            delayRepeat(R.drawable.buttonblueclicked, R.drawable.buttonblue);
        } else if (whichButton.get(i) == 4) {
            delayRepeat(R.drawable.buttonblueclicked, R.drawable.buttonblue);
        } else if (whichButton.get(i) == 5) {
            delayRepeat(R.drawable.buttonblueclicked, R.drawable.buttonblue);
        } else if (whichButton.get(i) == 6) {
            delayRepeat(R.drawable.buttonblueclicked, R.drawable.buttonblue);
        } else if (whichButton.get(i) == 7) {
            delayRepeat(R.drawable.buttonblueclicked, R.drawable.buttonblue);
        } else if (whichButton.get(i) == 8) {
            delayRepeat(R.drawable.buttonblueclicked, R.drawable.buttonblue);
        } else if (whichButton.get(i) == 9) {
            delayRepeat(R.drawable.buttonblueclicked, R.drawable.buttonblue);
        }
        System.out.println("Which button size and number is "
                + whichButton.size());
        System.out.println(i);
    }
}

// For repeating back the buttons
// On start this method sets the button to the color and waits 1 second
// On finish it changes back to the original image
public void delayRepeat(final int newStartID, final int endID) {
    final int time = 1000;
    new CountDownTimer(time, 1000) {

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
            for (int i = 0; i < whichButton.size(); i++) {
                if (whichButton.get(i) == 1) {
                    buttonRed.setBackgroundResource(endID);
                } else if (whichButton.get(i) == 2) {
                    buttonYellow.setBackgroundResource(endID);
                } else if (whichButton.get(i) == 3) {
                    buttonOrange.setBackgroundResource(endID);
                } else if (whichButton.get(i) == 4) {
                    buttonBlack.setBackgroundResource(endID);
                } else if (whichButton.get(i) == 5) {
                    buttonGreen.setBackgroundResource(endID);
                } else if (whichButton.get(i) == 6) {
                    buttonPurple.setBackgroundResource(endID);
                } else if (whichButton.get(i) == 7) {
                    buttonPink.setBackgroundResource(endID);
                } else if (whichButton.get(i) == 8) {
                    buttonLime.setBackgroundResource(endID);
                } else if (whichButton.get(i) == 9) {
                    buttonDarkBlue.setBackgroundResource(endID);
                }
            }
            new CountDownTimer(time, 1000) {
                @Override
                public void onFinish() {
                    for (int i = 0; i < whichButton.size(); i++) {
                        // TODO Auto-generated method stub
                        if (whichButton.get(i) == 1) {
                            buttonRed.setBackgroundResource(newStartID);
                        } else if (whichButton.get(i) == 2) {
                            buttonYellow.setBackgroundResource(newStartID);
                        } else if (whichButton.get(i) == 3) {
                            buttonOrange.setBackgroundResource(newStartID);
                        } else if (whichButton.get(i) == 4) {
                            buttonBlack.setBackgroundResource(newStartID);
                        } else if (whichButton.get(i) == 5) {
                            buttonGreen.setBackgroundResource(newStartID);
                        } else if (whichButton.get(i) == 6) {
                            buttonPurple.setBackgroundResource(newStartID);
                        } else if (whichButton.get(i) == 7) {
                            buttonPink.setBackgroundResource(newStartID);
                        } else if (whichButton.get(i) == 8) {
                            buttonLime.setBackgroundResource(newStartID);
                        } else if (whichButton.get(i) == 9) {
                            buttonDarkBlue
                                    .setBackgroundResource(newStartID);
                        }
                    }
                }

                @Override
                public void onTick(long arg0) {
                    // TODO Auto-generated method stub
                }
            }.start();
        }

        @Override
        public void onTick(long arg0) {
            // TODO Auto-generated method stub
        }
    }.start();
}

// creating a blinking color button for each specific random number
// On start this method sets the button to the color and waits 1 second
// On finish it changes back to the original image
public void delay(final int newStartID, final int endID) {
    final int time = 1000;
    new CountDownTimer(time, 1000) {

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
            if (n == 1) {
                buttonRed.setBackgroundResource(endID);
            } else if (n == 2) {
                buttonYellow.setBackgroundResource(endID);
            } else if (n == 3) {
                buttonOrange.setBackgroundResource(endID);
            } else if (n == 4) {
                buttonBlack.setBackgroundResource(endID);
            } else if (n == 5) {
                buttonGreen.setBackgroundResource(endID);
            } else if (n == 6) {
                buttonPurple.setBackgroundResource(endID);
            } else if (n == 7) {
                buttonPink.setBackgroundResource(endID);
            } else if (n == 8) {
                buttonLime.setBackgroundResource(endID);
            } else if (n == 9) {
                buttonDarkBlue.setBackgroundResource(endID);
            }

            new CountDownTimer(time, 1000) {

                @Override
                public void onFinish() {
                    // TODO Auto-generated method stub
                    if (n == 1) {
                        buttonRed.setBackgroundResource(newStartID);
                    } else if (n == 2) {
                        buttonYellow.setBackgroundResource(newStartID);
                    } else if (n == 3) {
                        buttonOrange.setBackgroundResource(newStartID);
                    } else if (n == 4) {
                        buttonBlack.setBackgroundResource(newStartID);
                    } else if (n == 5) {
                        buttonGreen.setBackgroundResource(newStartID);
                    } else if (n == 6) {
                        buttonPurple.setBackgroundResource(newStartID);
                    } else if (n == 7) {
                        buttonPink.setBackgroundResource(newStartID);
                    } else if (n == 8) {
                        buttonLime.setBackgroundResource(newStartID);
                    } else if (n == 9) {
                        buttonDarkBlue.setBackgroundResource(newStartID);
                    }
                }

                @Override
                public void onTick(long arg0) {
                    // TODO Auto-generated method stub

                }

            }.start();

        }

        @Override
        public void onTick(long arg0) {
            // TODO Auto-generated method stub

        }

    }.start();

}

//Used to generate a different random number each time
public int randomNumber() {
    randNumber = new Random();
    int n = randNumber.nextInt(9) + 1;
    return n;
}

// On click for when they click each button
// Buttons are set to specific tags in the onCreate
// Checks if the tag is equal to what the output of whichButton.get(x) is
// If it isn't they fail
public void onClick(View v) {
    // TODO Auto-generated method stub
    int tag = (Integer) v.getTag();
    for (int x = 0; x < whichButton.size(); x++) {
        if (tag == 1 && tag == whichButton.get(x)) {
            aiTurn = true;
            whenStarted();
        } else if (tag == 2 && tag == whichButton.get(x)) {
            aiTurn = true;
            whenStarted();
        } else if (tag == 3 && tag == whichButton.get(x)) {
            aiTurn = true;
            whenStarted();
        } else if (tag == 4 && tag == whichButton.get(x)) {
            aiTurn = true;
            whenStarted();
        } else if (tag == 5 && tag == whichButton.get(x)) {
            aiTurn = true;
            whenStarted();
        } else if (tag == 6 && tag == whichButton.get(x)) {
            aiTurn = true;
            whenStarted();
        } else if (tag == 7 && tag == whichButton.get(x)) {
            aiTurn = true;
            whenStarted();
        } else if (tag == 8 && tag == whichButton.get(x)) {
            aiTurn = true;
            whenStarted();
        } else if (tag == 9 && tag == whichButton.get(x)) {
            aiTurn = true;
            whenStarted();
        } else {
            aiTurn = false;
            DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    switch (which) {
                    case (DialogInterface.BUTTON_POSITIVE):
                        aiTurn = true;
                        whenStarted();
                        break;
                    case (DialogInterface.BUTTON_NEGATIVE):

                        break;
                    }
                }
            };
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("You failed, would you like to play again?")
                    .setPositiveButton("Yes", dialogClickListener)
                    .setNegativeButton("No", dialogClickListener).show();

        }
    }
}

}

```