Tell me how in Android to change the color of the button when you click on it. The color should be changed "in a circle", there should be more than two colors.

  • similar to the test task, add your current work, show where you have specific difficulties. - Komdosh
  • Not test, found in the tutorial on android. The difficulty is how to make this change. - Sergey Anisko
  • one

1 answer 1

The idea is the following: you create an array of colors and its index. By clicking you change the index to the next and pull out the color from the array of colors.

private int currentColor = 0; private int[] colors = {0xFFFF0000, 0xFF00FF00, 0xFF0000FF}; button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { currentColor = (currentColor+1) % colors.length; ((Button) v).setBackgroundColor(colors[currentColor]); } });