0

I am having a problem with a Gomoku program I am making (5-in a row on a 10x10 board). I am trying to implement a 10x10 array of buttons from my Game.java to my game.xml. Here is the code I currently have

   public class Game extends Activity implements View.OnClickListener{
    private boolean p2Turn = false;
    private char board[][] = new char[10][10];
    Context c;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);
        Button btn[][] = new Button[10][10];
        for(int i = 0; i<10; i++){
            for(int j = 0; j<10; j++){
                btn [i][j] = new Button(this);

            }

        }

    }
}

However I don't know how to implement the 10x10 button array to my game.xml

Help would be great :D

Mike Rotch
  • 11
  • 2

2 Answers2

1

The Buttons are created but placed in no where. this may help

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_activity);
    final LinearLayout container = (LinearLayout)findViewById(R.id.container where you want to place your buttons);

    Button btn[][] = new Button[10][10];
    for(int i = 0; i<10; i++){
        for(int j = 0; j<10; j++){
            btn [i][j] = new Button(this);
            btn[i][j].setText("Button "+i);

            container.addView(btn[i][j],i);

        }

    }

}
Tobiel
  • 1,543
  • 12
  • 19
0

add buttons to the layout...

ViewGroup layout = (ViewGroup) findViewById(R.layout.game);
     Button btn[][] = new Button[10][10];
      for(int i = 0; i<10; i++){
           for(int j = 0; j<10; j++){
             btn [i][j] = new Button(this);
              layout.addView(btn [i][j]);
         }

    }
Shiva
  • 201
  • 1
  • 2
  • 9