Hermes_Lee
Hermes_Lee

Reputation: 5

How to execute a runnable after another in Android for a set of buttons?

I'm pretty new to Android. My goal is to make each of those 4 buttons flash red (and back to default) one after another. button0 flash for a second, and switch back to default color. button1 flash for a second, and switch back to default color. etc... till full sequence is finished. For that reason I created a loop for handlers and 4 different runnables for each button (Could not come up with a mechanism to solve the problem with one runnable only for all buttons) My code does something else than I'm expecting.It lights up red button0 for 2 seconds (approx) and button2 for a second, no flashing at all happens with button1 and button3, but they go through loop cycle as they all turn to btn_default_small style. also The btn_default_small is not exactly what default button looks like.

public class MainActivity extends ActionBarActivity {

    Button button0;
    Button button1;
    Button button2;
    Button button3;
    private Handler handler;
    int count=0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button0 = (Button) findViewById(R.id.button0);
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);




        handler = new Handler();
        for(int x=0;x<=3;x++){
            switch(x){
            case 0:
                handler.postDelayed(runnableButton0, 500);
                break;
            case 1:
                handler.postDelayed(runnableButton1, 500);
                break;  
            case 2:
                handler.postDelayed(runnableButton2, 500);
                break;
            case 3:
                handler.postDelayed(runnableButton3, 500);
                break;
            }
        };

    }

    private void LightButton(Button b, Runnable r) {

        if(count==0){
            b.getBackground().setColorFilter(new LightingColorFilter(0xff0000, 0xff0000));//Change button color to red
            count++;
            handler.postDelayed(r, 700);
        }
        else{
            b.setBackgroundResource(android.R.drawable.btn_default_small);//Change button color to default
            count=0;
        }
    }

    Runnable runnableButton0= new Runnable() {
        @Override
        public void run() {
            LightButton(button0, runnableButton0);
        }
    };  

    Runnable runnableButton1= new Runnable() {
        @Override
        public void run() {
            LightButton(button1, runnableButton1);
        }
    };  
    Runnable runnableButton2= new Runnable() {
        @Override
        public void run() {
            LightButton(button2, runnableButton2);
        }
    };  
    Runnable runnableButton3= new Runnable() {
        @Override
        public void run() {
            LightButton(button3, runnableButton3);
        }
    };  

What am I doing wrong or what am I not understanding about handler and runnable action cycle?

Upvotes: 0

Views: 1441

Answers (1)

user
user

Reputation: 87064

What am I doing wrong or what am I not understanding about handler and runnable action cycle?

First of all you post all those runnables at the same time and you also share the count variable(which you shouldn't so) between them which will make some of them not changing background.

private Buttons[] btns =  /* an array holding all your 4 buttons*/
private int count = 0;
Runnable runMe = new Runnable() {
    @Override
    public void run() {
        if (count == btns.length) {
            // we iterated over the entire btns array so abort
            btns[count - 1].getBackground.clearColorFilter();
            return;
        }
        if (count >= 1) {
             // change the previous button back to normal only if
             // we are at the second button
             btns[count - 1].getBackground.clearColorFilter();
        } 
        // change the background
        btns[count].getBackground().setColorFilter(new LightingColorFilter(0xff0000, 0xff0000));
        // increase count to iterate over the btns array
        count++;
        // schedule the next step
        handler.postDelayed(this, 1000);
    }
};  

// in onCreate(),start the flashing with a 1 second delay
handler.postDelayed(runMe, 1000);

The code above should, after an initial delay of 1 second, change the background of the first button(to the color) then after 1 second change the background of this first button to normal and the background of the second button to the color(and so on).

Upvotes: 3

Related Questions