I am new to android development. I am trying to create a tic tac toe game, and currently I want my buttons to be labelled with an X on click but when I run the program and click a button nothin happens. My activity.java file is below:
package com.android.tictactoe;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class TictactoeActivity extends Activity implements Button.OnClickListener{
Button one, two, three, four, five, six, seven, eight, nine;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
one=(Button)findViewById(R.id.one);
one.setOnClickListener(this);
two=(Button)findViewById(R.id.two);
two.setOnClickListener(this);
three=(Button)findViewById(R.id.three);
three.setOnClickListener(this);
four=(Button)findViewById(R.id.four);
four.setOnClickListener(this);
five=(Button)findViewById(R.id.five);
five.setOnClickListener(this);
six=(Button)findViewById(R.id.six);
six.setOnClickListener(this);
seven=(Button)findViewById(R.id.seven);
seven.setOnClickListener(this);
eight=(Button)findViewById(R.id.eight);
eight.setOnClickListener(this);
nine=(Button)findViewById(R.id.nine);
nine.setOnClickListener(this);
}
public void OnClick(Button button, int CheckedId){
if(CheckedId==R.id.one && button.getText().equals("")){
button.setText("X");
}
else if(CheckedId==R.id.two && button.getText().equals("")){
button.setText("X");
}
else if(CheckedId==R.id.three && button.getText().equals("")){
button.setText("X");
}
else if(CheckedId==R.id.four && button.getText().equals("")){
button.setText("X");
}
else if(CheckedId==R.id.five && button.getText().equals("")){
button.setText("X");
}
else if(CheckedId==R.id.six && button.getText().equals("")){
button.setText("X");
}
else if(CheckedId==R.id.seven && button.getText().equals("")){
button.setText("X");
}
else if(CheckedId==R.id.eight && button.getText().equals("")){
button.setText("X");
}
button.setText("X");
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
my main.xml is below:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="0" >
<TableRow>
<TextView
android:text="Fizzle TicTacToe"
android:textStyle="bold"
android:textSize="30dip"
android:textColor="#ffaa88"
/>
</TableRow>
<TableRow>
<Button
android:textColor="#440000"
android:textSize="40dip"
android:textStyle="bold"
android:text=""
android:id="@+id/one"
android:layout_span="1"
android:layout_width="0dip"
android:layout_height="100dip"
android:layout_weight="30"/>
<Button
android:textColor="#440000"
android:textSize="40dip"
android:textStyle="bold"
android:text=""
android:id="@+id/two"
android:layout_span="1"
android:layout_width="0dip"
android:layout_height="100dip"
android:layout_weight="30"/>
<Button
android:textColor="#440000"
android:textSize="40dip"
android:textStyle="bold"
android:text=""
android:id="@+id/three"
android:layout_span="1"
android:layout_width="0dip"
android:layout_height="100dip"
android:layout_weight="30"/>
</TableRow>
<TableRow>
<Button
android:textColor="#440000"
android:textSize="40dip"
android:textStyle="bold"
android:text=""
android:id="@+id/four"
android:layout_span="1"
android:layout_width="0dip"
android:layout_height="100dip"
android:layout_weight="30"/>
<Button
android:textColor="#440000"
android:textSize="40dip"
android:textStyle="bold"
android:text=""
android:id="@+id/five"
android:layout_span="1"
android:layout_width="0dip"
android:layout_height="100dip"
android:layout_weight="30"/>
<Button
android:textColor="#440000"
android:textSize="40dip"
android:textStyle="bold"
android:text=""
android:id="@+id/six"
android:layout_span="1"
android:layout_width="0dip"
android:layout_height="100dip"
android:layout_weight="30"/>
</TableRow>
<TableRow>
<Button
android:textColor="#440000"
android:textSize="40dip"
android:textStyle="bold"
android:text=""
android:id="@+id/seven"
android:layout_span="1"
android:layout_width="0dip"
android:layout_height="100dip"
android:layout_weight="30"/>
<Button
android:textColor="#440000"
android:textSize="40dip"
android:textStyle="bold"
android:text=""
android:id="@+id/eight"
android:layout_span="1"
android:layout_width="0dip"
android:layout_height="100dip"
android:layout_weight="30"/>
<Button
android:textColor="#440000"
android:textSize="40dip"
android:textStyle="bold"
android:text=""
android:id="@+id/nine"
android:layout_span="1"
android:layout_width="0dip"
android:layout_height="100dip"
android:layout_weight="30"/>
</TableRow>
</TableLayout>
2 Answers 2
You need to provide the onClick behaviour on the overrided method not your own method.
@Override
public void onClick(View v) {
OnClick((Button)v, v.getId());
}
N J
27.5k14 gold badges80 silver badges97 bronze badges
answered Jun 28, 2015 at 12:28
Ahmed Hegazy
12.7k5 gold badges45 silver badges66 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Your OnClick method is not valid, copy his content to onClick method with overide
Comments
lang-java