java – Suspend a loop with the CountDownTimer methods until the methods are completed


I am trying to create an application that mimics a traffic light by looping a segment of code that changes the background of a TextView from red to yellow to green after holding each color for a time. The user will be able to start and end the loop by pressing a button. I have used CountDownTimers to keep each TextView background for a while, and this method seems to work fine. The current status of my code is as follows:

    public void trafficLight() {
        lightDisplay.setBackgroundColor(Color.RED);

        // Temporary for loop, a do-while loop causes the app to crash
        for (int x = 2; x <= 4; x++) {
             signalSwitcher(3000, Color.YELLOW);
             signalSwitcher(6000, Color.GREEN);
             signalSwitcher(9000, Color.RED);
        }
    }


    public void signalSwitcher(long delayTime, final int signalColor) {
        new CountDownTimer(delayTime, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
            }

            @Override
            public void onFinish() {
                lightDisplay.setBackgroundColor(signalColor);
            }

        }.start();

I am unable to loop through the trafficLight method code, as I have not found a way to suspend the start of another loop iteration before the end of the change cycle. 39; background. In other words, when I run the code, the loop continues to schedule tasks in the CountDownTimers; he doesn't plan, wait for tasks to finish and try again.

My best attempt to try to solve this problem was to have another do-while loop at the end of the signalSwitcher methods which rechecks a parameter until the last signalSwitcher method provides the input necessary to exit this loop. However, it seems that this method does not work, because I get a blank screen when I try to run the application.

Simply put, how can you suspend a loop until the code inside CountDownTimers is executed?