To repeat the flowers -
Button btn = (Button) findViewById(R.id.btn);
Handler handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
int i = 0;
if (i == 0) {
btn.setBackgroundColor(Color.YELLOW);
i++;
} else if (i == 1) {
btn.setBackgroundColor(Color.RED);
i++;
} else if (i == 2) {
btn.setBackgroundColor(Color.BLUE);
i++;
} else if (i == 3) {
btn.setBackgroundColor(Color.GREEN);
i = 0;
}
handler.postDelayed(this, 3000);
}
};
handler.postDelayed(r, 3000);
This code changes the color of the button every 3 seconds in this order - yellow, red, blue, green.
For RANDOM colors -
Button btn = (Button) findViewById(R.id.btn);
Handler handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
int i = (int) Math.random() * 3;
if (i == 0) {
btn.setBackgroundColor(Color.YELLOW);
} else if (i == 1) {
btn.setBackgroundColor(Color.RED);
} else if (i == 2) {
btn.setBackgroundColor(Color.BLUE);
} else if (i == 3) {
btn.setBackgroundColor(Color.GREEN);
}
handler.postDelayed(this, 3000);
}
};
handler.postDelayed(r, 3000);
If you like this answer, mark it as selected.