Search code examples
android-studiobuttontableviewtic-tac-toe

Get all Buttons In table layout in android Studio


I am trying to implement a tic tac toe game in android to learn the basics. The approach I want to implement the logic is given below.

  1. Check if any button has still default text., If yes, then we can still play the game, and boxes are not entirely filled yet. (for this, I want to check the text of all buttons in my table view. I think I can get it through somehow making an array list of all buttons).

enter image description here

I want all these buttons in an arraylist. Here is my hierarchy

enter image description here

I have got idea from this SOF page and tried to do this.

protected void isboxEmpty()
    {
        for (int i = 1; i <=9 ; i++)
        {
            int id = getResources().getIdentifier("button" + i, "id", getPackageName());
            button[i] = (Button) findViewById(id);
            Log.d("poi",button[i].getText().toString());
        }
    }

But I don't know what it does and how. Please help me with this.


Solution

  • protected void isboxEmpty()
        {
            for (int i = 1; i <=9 ; i++)/psoo-s
            {
                int id = getResources().getIdentifier("button" + i, "id", getPackageName());
                button[i] = (Button) findViewById(id);
                Log.d("poi",button[i].getText().toString());
            }
        }
    

    what does this do you ask, it loops through the box(which is probably an array in the java class for all the buttons)

    the int id gets the button 1-9 from your XML file and that will be how you place the buttons into the array. make sure the names of the buttons in the XML file are button1 or button2 or button3...

    the button[i]-so i is 1-9 and your saying with the findviewbyid is that look for this button and places it in the array at that specific placement-i(1-9).

    button is a data type like integers, booleans...

    the log.d will print in one of the terminals so you are not actually placing the buttons from the XML into your array in the java class. if you want your loop to work then change it to this

            buttonrestart = (Button) findViewById(R.id.buttonrestart);
            for(int i=0;i<button.length;i++){
                int resID= getResources().getIdentifier("button"+i,"id", getPackageName());
                button[i]=findViewById(resID);
                button[i].setOnClickListener(this);//add this because you want something to happen when you click on the button right?
            }
    

    on the setOnClickListener(this) make sure you implement the onClick function for TicTackToe I would advise using the MVC(Model-View-Controller) java classes for your code. good luck let me know if you need anything else. and in the onclick function is where you will apply your logic for what happens to the board- does an x or o appear and did someone win or is there a draw or whos turn is it?

    EDIT:
    you can't get an ArrayList unless one already exists! therefore you need to create an array first, and a regular button array is good enough here. here is a button array, the private tells you that it only belongs to this class. in your instance it's not to important so you can take off the private but if you do encounter a problem due to it, then feel free to read the private. the Button[] tells you its type Button, button is the name, new button tells you that you are creating a new array. The 9 tells you that the length of the array is 9. remember index's start from 0 and go to 9(not including index 9) so the indexes are: 1 2 3 4 5 6 7 8

    private Button[] button= new Button[9];

    a normal integer array would look like the following:

    int[] arr = new int[9];

    can you see the structure similarities between the button array and the regular integer array?

    this code goes right after the

    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    

    or

    public class MainActivity extends AppCompatActivity