Nexo
Nexo

Reputation: 23

How can I delay UI code on Android?

I will preface this with me scouring for an answer, and finding similar questions here, but have not found one that works in my case. :(

I am trying to create a *Simon Says*esque Android game in Eclipse, and I have created a method to choose a random color (adding to an arraylist of previous chosen colors). The array would looks like {"Blue", "Green", "Yellow"}, etc. Once the color is added, I want to be able to loop through the table and "flash" the respective button to signify that is the correct answer. For example, if the chosen colors are blue, green, and yellow, it would do:

flash blue

flash green

flash yellow

//"flashing" would just be changing the ImageButton's background

However, I want to be able to "delay" the flashing of upcoming colors, as without delay, I wouldn't be able to tell what flashed, as all of them would flash instantaneously. Is there anyway to do what I'm describing? I've tried thread.Sleep(), and I don't think that's what I want as it freezes the screen. I've also heard of Runnables and Timers, but I don't know if that is applicable in this case or how to implement it.

I am using a method, changeImage(), to "flash" the colors. Something like:

public void chooseColor() {

//add color to arraylist

    for (String currentColor : colorarraylist) {

    changeImage();

    //delay

    changeImage();

    }

}

I have been struggling to solve this problem, any help would be appreciated.

Full method:

public void chooseColor() {

    chooseOn = false;

    //Adds random color to answerList
    String randomColor = colorChoices[(int)(Math.random()*5)];
    answerList.add(randomColor);
    Log.v("", "ChosenColor: " + randomColor);

    for (String Answer : answerList) {
         ImageButton selectedButton;
        Log.v("", "Answer: " + Answer);

        playSound(R.raw.beep1);

        //Set selectedButton to current color in answerList
        int buttonID = getResources().getIdentifier(Answer.toLowerCase() + "_Button", "id", getPackageName());
        selectedButton = (ImageButton) findViewById(buttonID);

        //Set selectedButton's color (background) to look highlighted
        changeImage(Answer.toLowerCase() + "light", selectedButton);


        changeImage(Answer.toLowerCase(), selectedButton);


    }
    chooseOn = true;

}

Upvotes: 0

Views: 5273

Answers (2)

Gowtham Raj
Gowtham Raj

Reputation: 2955

Try this code... Hope it helps

  Thread timer = new Thread(){
        @Override
        public void run() {

            try {
                sleep(1000);//time in milliseconds
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally{
                //Do something after timer runs out or call some functions
            }
            super.run();
        }
    };
    timer.start();

Upvotes: 2

Benjamin Sergent
Benjamin Sergent

Reputation: 223

You could put logic in a thread and sleep between iterations of the loop. You'd need to make a runnable to put on the UI thread when changing the image.

If I'm understanding your full method properly, something like this should work:

private int colorFlashTime = 500;
private int colorOffTime = 100;

....
public void chooseColor() {

    //Adds random color to answerList
    String randomColor = colorChoices[(int)(Math.random()*5)];
    answerList.add(randomColor);
    Log.v("", "ChosenColor: " + randomColor);

    Thread colorThread = new Thread() {
        public void run() {

            chooseOn = false;
            for (final String Answer : answerList) {

                Runnable changeImageRunnableLight = new Runnable() {
                    public void run() {
                        ImageButton selectedButton;
                        Log.v("", "Answer: " + Answer);

                        playSound(R.raw.beep1);

                        //Set selectedButton to current color in answerList
                        int buttonID = getResources().getIdentifier(Answer.toLowerCase() + "_Button", "id", getPackageName());
                        selectedButton = (ImageButton) findViewById(buttonID);

                        //Set selectedButton's color (background) to look highlighted
                        changeImage(Answer.toLowerCase() + "light", selectedButton);
                    }

                };

                runOnUiThread(changeImageRunnableLight);

                try {
                    Thread.sleep(colorFlashTime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                Runnable changeImageRunnable = new Runnable() {
                    public void run() {
                        changeImage(Answer.toLowerCase(), selectedButton);
                    }

                };

                runOnUiThread(changeImageRunnable);

                try {
                    Thread.sleep(colorOffTime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }

            chooseOn = true;
        }

    };

    colorThread.start();
}

Upvotes: 2

Related Questions