I have nine buttons in 3 X 3 grid for tic-tac-toe app. Here is the code from board_layout.xml for first three buttons -
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20sp" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:id="@+id/cellOne"
android:layout_width="90sp"
android:layout_height="98sp"
android:text=""
android:textSize="70sp"
/>
<Button
android:id="@+id/cellTwo"
android:layout_width="90sp"
android:layout_height="98sp"
android:text=""
android:textSize="70sp"
/>
<Button
android:id="@+id/cellThree"
android:layout_width="90sp"
android:layout_height="98sp"
android:text=""
android:textSize="70sp"
/>
</TableRow>
<!and so on for other 6>
Now in my MainActivity.java I have a function setboard() that sets this layout on this screen and initializes the buttons as follows:
private String p1name = "P1";
private String p2name = "P2";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void setName(View view) {
//Function called upon click on some button
// Other code irrelevant to the question
setBoard();
}
}
public void setBoard() {
////////////
LayoutInflater inflater = this.getLayoutInflater();
View boardView = inflater.inflate(R.layout.board_layout, null);
////////////
boardCells = new Button[3][3];
boardCells[0][0] = (Button) boardView.findViewById(R.id.cellOne);
boardCells[0][1] = (Button) boardView.findViewById(R.id.cellTwo);
boardCells[0][2] = (Button) boardView.findViewById(R.id.cellThree);
// Code for other 6 buttons
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
boardCells[i][j].setOnClickListener(new MyClickListener(i, j));
boardCells[i][j].setText("");
boardCells[i][j].setEnabled(false);
}
setContentView(R.layout.board_layout);
TextView tv1 = (TextView) findViewById(R.id.p1Name_board);
tv1.setText(p1name + ":");
tv1 = (TextView) findViewById(R.id.p2Name_board);
tv1.setText(p2name + ":");
turnDisp = (TextView) findViewById(R.id.turnDispString);
turnDisp.setText("Turn of " + p1name);
}
Here is the code for MyClickListener class (inner class of MainActivity class):
class MyClickListener implements View.OnClickListener {
int x;
int y;
public MyClickListener(int x, int y) {
this.x = x;
this.y = y;
//This Log.d works fine
Log.d("TAG1", Float.toString(x) + " " + Float.toString(y));
}
public void onClick(View view) {
//This Log.d doesn't work
Log.d("TAG2", Float.toString(this.x) + " Hi ");
//This Toast also doesn't work
Toast.makeText(getApplicationContext(), "Pressed", Toast.LENGTH_LONG).show();
//Plus Other code irrelevant to the question
}
Now the issue is that Log.d in constructor called while setting buttons is working fine and prints to Logcat but the onClick function doesn't works when the button is clicked. Neither the Log.d nor the toast is shown for onClick function.
Any help would be appreciated.