Zun Jie
Zun Jie

Reputation: 45

Xamarin IF statement to match image button and it's background

I am currently following up on this problem here: Setting Image Button via it's id into a 8x8 2d array. Android. Xamarin.

I have successfully set the 64 image button's id into an 8x8 array. In my image button, there are some buttons with an image on the button with the following code:

        android:src="@android:drawable/ic_menu_gallery"
        android:background="@drawable/white"

There are other image buttons that are blank with no image. My question is: Of all my 64 image button, I would like to count how many buttons that has a background image white, based on the code I have above.

Currently I have the following code:

        for (int i = 0; i < 8; i++) //Rows of the array "buttons"
        {
            for (int j = 0; j < 8; j++) //Column of the array "buttons"
            {
                if(buttons[i, j] ==  ....???) //Check if the id of the image button has a white drawable image.
            }
        }

What should I input to finish up the code?

Upvotes: 1

Views: 397

Answers (1)

Aeneas Motsch
Aeneas Motsch

Reputation: 120

In C#.NET I try to put these kind of information in Tags. To use Tags in Android you have to create a String resource in values/Strings.xml. This is used to make sure, that the tag is unique.

<?xml version="1.0" encoding="utf-8"?>
  <resources>
    <string name="ColorOfButton">color</string>
  </resources>

When you initialize the buttons you can create a tag like so:

buttons[i, j].SetTag(Resource.String.ColorOfButton, "white");
//sets it to white

Or in XML:

<Button
    android:id="@+id/testBtn">
    <tag android:id="@string/ColorOfButton" android:value="white" />
</Button>
<Button
    android:id="@+id/testBtn2">
    <tag android:id="@string/ColorOfButton" android:value="black" />
</Button>

Then you can get the tag and look if it is white, black or empty:

switch(buttons[i, j].GetTag(Resource.String.ColorOfButton).ToString()) 
{
  case "white":
    //Your picture is white
  break;
  case "black":
    //Your picture is black
  break;
  case "empty":
    //Your picture is empty
  break;
}

You can put the following strings in your tag:

white - for white piece

black - for black piece

empty - for no piece

Upvotes: 2

Related Questions