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

4
  • What's the problem you're having with your code? Commented Jul 18, 2013 at 23:49
  • The button array doesn't show up Commented Jul 18, 2013 at 23:50
  • 1
    You're going to want to take a look at this: stackoverflow.com/questions/4907609/… Commented Jul 18, 2013 at 23:54
  • So you would be having 100 onClickListeners then? Commented Jul 19, 2013 at 0:13

2 Answers 2

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);

        }

    }

}
Sign up to request clarification or add additional context in comments.

Comments

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]);
         }

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.