0

I have a switch statment that changes the colour of certain buttons if true. The problem is that if more than one button is being lit, they all light at the same time. I need the code to pause after for a second after every case that is true.

public void PlaySequence() {

    //loops through arraylist and changes the background colour of the buttons that has the corresponding number assigned i.e. red = 0, blue = 1 etc.
    for (int i = 0; i < yourList.size(); i++) {
        switch (Integer.parseInt(yourList.get(i).toString())) {
            case 0:

                redButton.setBackgroundColor(Color.RED);
                revertButtonColour(0);
                break;
            case 1:
                blueButton.setBackgroundColor(Color.BLUE);
                revertButtonColour(1);
                break;

            case 2:
                greenButton.setBackgroundColor(Color.GREEN);
                revertButtonColour(2);
                break;
            case 3:
                yellowButton.setBackgroundColor(Color.YELLOW);
                revertButtonColour(3);
                break;
        }
    }

    Toast toast = Toast.makeText(this.getApplicationContext(), "Go!", Toast.LENGTH_SHORT);
    toast.show();
}
Haldamir
  • 115
  • 2
  • 10
  • 1
    You can put `Thread.sleep(1000);` in your loop if that's *really* what you want. Note that this will pause the UI Thread making it unusable for that second – codeMagic Dec 01 '15 at 13:57
  • Thread.sleep(1000) ? – JFPicard Dec 01 '15 at 13:58
  • Integer.parseInt(yourList.get(i).toString()) That's the magic!=)) – Android Android Dec 01 '15 at 14:00
  • 1
    [How to pause/sleep Thread](http://stackoverflow.com/questions/1520887/how-to-pause-sleep-thread-or-process-in-android) – codeMagic Dec 01 '15 at 14:03
  • @weston that type of response is really not constructive. That's why I didn't put it as an answer, left a note about what it would actually do, and linked to a post showing other ways. – codeMagic Dec 01 '15 at 14:09
  • 1
    @codeMagic why give an suggestion you wouldn't actually do? OP, that may appear to work, but you should not do it. It's not just one second, it's 1sec x `yourList.size()`, that will cause an [ANR](http://stackoverflow.com/questions/6540076/is-anr-exception-or-error-or-what) – weston Dec 01 '15 at 14:12

2 Answers2

0

You should put that code in a separate thread, then make use of runOnUIThread() to change the buttons background.

Otherwise you will be calling sleep multiple times so your changes to the buttons will not be drawn as the UI Thread is sleeping.

Just put your code inside the run method of a runnable and start it like this:

new Thread(new Runnable(){ //here your run() method }).start();

To make use of runOnUIThread you will need a reference to your activity.

Hope this helps.

Nanoc
  • 2,381
  • 1
  • 20
  • 35
0

Extract this method:

void setColour(String colour) {
    switch (Integer.parseInt(colour)) {
        case 0:
            redButton.setBackgroundColor(Color.RED);
            revertButtonColour(0);
            break;
        case 1:
            blueButton.setBackgroundColor(Color.BLUE);
            revertButtonColour(1);
            break;
        case 2:
            greenButton.setBackgroundColor(Color.GREEN);
            revertButtonColour(2);
            break;
        case 3:
            yellowButton.setBackgroundColor(Color.YELLOW);
            revertButtonColour(3);
            break;
    }
}

Then use postDelayed to update.

public void PlaySequence() {
    //copy the current list to a queue
    final Queue<String> queue = new ArrayDeque<String>(yourList);

    if (queue.isEmpty()) return; //no sequence to show

    final Handler handler = new Handler();

    final Runnable showNextColour = new Runnable() { 
         public void run() { 
              setColour(queue.pop()); //set colour to queue head

              if (!queue.isEmpty()) { //any more?
                  // schedule next one for one seconds time
                  handler.postDelayed(showNextColour, 1000);
              }
         } 
    };

    showNextColour.run(); //show first colour now
}
weston
  • 54,145
  • 21
  • 145
  • 203