Android Multiple Button Color Changes
Solution 1:
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.
Solution 2:
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).
Solution 3:
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.
Post a Comment for "Android Multiple Button Color Changes"