Pedro Destri
Pedro Destri

Reputation: 93

Android multiple Button color changes

So, I have this problem, I'm trying to make a Button change it´s color, and the color of the other buttons that sorround it when i click on it, and it needs to stay like that, until some one clicks on it again, or until some on clicks on a button that sorrounds it. The picture makes it easier to understand. UI

so eg. when you click number 6, numbers 2,6,7,5,10 need to change its color to red, and, when you click on it again, they need to change it's color back to green. So, I did this:

 Button[] btns = new Button[16]; //colocando 16 botões nele

    btns[0] = (Button) findViewById(R.id.button1);
    btns[1] = (Button) findViewById(R.id.button2);
    btns[2] = (Button) findViewById(R.id.button3);
    btns[3] = (Button) findViewById(R.id.button4);

    btns[4] = (Button) findViewById(R.id.button5);
    btns[5] = (Button) findViewById(R.id.button6);
    btns[6] = (Button) findViewById(R.id.button7);
    btns[7] = (Button) findViewById(R.id.button8);

    btns[8] = (Button) findViewById(R.id.button9);
    btns[9] = (Button) findViewById(R.id.button10);
    btns[10] = (Button) findViewById(R.id.button11);
    btns[11] = (Button) findViewById(R.id.button12);

    btns[12] = (Button) findViewById(R.id.button13);
    btns[13] = (Button) findViewById(R.id.button14);
    btns[14] = (Button) findViewById(R.id.button15);
    btns[15] = (Button) findViewById(R.id.button16);

And now I'm a bit lost. I've built the game logic already on paper, the problem is, I don't know how to change the color of multiple buttons.

Upvotes: 0

Views: 1391

Answers (3)

jStaff
jStaff

Reputation: 710

If I were you I'd just set up the logic. Your picture would be easier to set up this structure if you started with 0 to 15 as well.

if you have a 4x4 grid as you show then which ever one is clicked would have:

  • x = number of button clicked.
  • n1 = x - 4
  • n2 = x + 4
  • n3 = x + 1
  • n4 = x - 1

Will all be Changed UNLESS they aren't greater than 0 or less than 16 ( 0 < n < 16) and if for just (x + 1) and (x - 1) as follows:

  • n3 % 4 == 0
  • n4 % 4 == 3
  • The 3 value comes from (4 - 1)

This algorithm could be extended to any x by x square grid by replacing the 4's with the x value as well.

Then if n3 or n4 don't get changed on the grid if they don't meet these requirements.

Thus have a function that accepts an int representing the number of the box clicked (x) and the btn grid itself, then apply this logic with x, n1, n2, n3, and n4 on the buttons clicked.

If those qualities above are met run another check for color and based off current color change it to the other.

Upvotes: 0

ishmaelMakitla
ishmaelMakitla

Reputation: 3812

Since you say you have the logic already, I guess you only need to be shown how to change the background color of the appropriate buttons. In this case, I'd have a method that takes an array of Button objects and the color to set them to:

private void changeColorOfTheseButtons(Button[] buttons, int color){
  for(int x=0; x < buttons.length; x++){
     //change its color
     buttons[x].setBackgroundColor(color);
   }
}

Then this is how you call the method above with a color (assuming you already have the list of buttons whose color you want to change):

...
changeColorOfTheseButtons(arrayOfButtonsToChange, Color.RED);

using color RED as example above - but you can use any color in Color.*

I hope this gives you some ideas.

Upvotes: 1

James Russo
James Russo

Reputation: 568

I would use a 2D array instead of a 1d array to do this. Then the logic is easy just get the buttons surrounding spot i,j would be (i-1,j-1),(i-1,j),(i-1,j+1), etc. You should easily be able to do this and add the appropriate logic for corner cases(aka buttons on the corners/edges).

Upvotes: 0

Related Questions