Mike Rotch
Mike Rotch

Reputation: 11

Adding java button to LinearLayout

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

Upvotes: 0

Views: 137

Answers (2)

Tobiel
Tobiel

Reputation: 1543

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

        }

    }

}

Upvotes: 1

Shiva
Shiva

Reputation: 201

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

    }

Upvotes: 0

Related Questions