error
doesn't work. tried playing it. x goes first then nothing happens. 0 doesn't move. click anywhere and it says space is not empty.
android:id="@+id/normalMode" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="51dp" android:background="@color/button" android:onClick="@string/normal" android:text="@string/normalText" /> android:id="@+id/UltimateMode" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/normalMode" android:layout_below="@+id/normalMode" android:layout_marginTop="41dp" android:background="@color/button" android:onClick="@string/ultimate" android:text="@string/ultimateText" /> android:id="@+id/AboutBtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/UltimateMode" android:layout_below="@+id/UltimateMode" android:layout_marginTop="42dp" android:background="@color/button" android:onClick="@string/about" android:text="@string/aboutText" /> android:id="@+id/Exit" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/AboutBtn" android:layout_below="@+id/AboutBtn" android:layout_marginTop="41dp" android:background="@color/button" android:onClick="@string/exit" android:text="@string/exitText" />
android:id
- is an unique identifier for each element.
android:layout_width
- the width of the button
android:layout_height
- the height of the button
android:onClick
- this attribute specifies which method in the activity java file will handle the click on the button. For example, if we specify this attribute in the next way:
android:onClick="@string/exit"
a method with the name, specified in the string resource file with tag "exit" will handle the click event. This entry in the resource file is:
<string name="exit">exit_click</string>
{ Intent myIntent = new Intent(MainActivity.this,NormalActivity.class); MainActivity.this.startActivity(myIntent); }
{ AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this); dlgAlert.setMessage("Under construction"); dlgAlert.setTitle("comming soon"); dlgAlert.setCancelable(true); dlgAlert.create().show(); }
{ Intent myIntent = new Intent(MainActivity.this, About.class); MainActivity.this.startActivity(myIntent); }
{ AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this); dlgAlert.setMessage("Do you really want to exit"); dlgAlert.setTitle("Exit"); dlgAlert.setCancelable(true); dlgAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); } }); dlgAlert.create().show(); }
dlgAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); } });
private int[][] table; private boolean xMove;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="0dp" android:background="@color/background" android:gravity="center_horizontal" android:label="@string/normal_activity_label" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".NormalActivity" > <TextView android:id="@+id/moveTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginLeft="34dp" android:layout_marginTop="18dp" android:gravity="center_horizontal" android:text="@string/x_move" android:textAppearance="?android:attr/textAppearanceLarge" /> <TableLayout android:id="@+id/tableLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/moveTextView" android:layout_marginTop="32dp" android:gravity="center_vertical|center_horizontal" > <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" > android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:height="100dp" android:onClick="@string/makeMove" android:text="@string/empty" android:width="100dp" /> android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="@string/makeMove" android:text="@string/empty" android:height="100dp" android:width="100dp" /> android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="@string/makeMove" android:text="@string/empty" android:height="100dp" android:width="100dp"/> </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content" > android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="@string/makeMove" android:text="@string/empty" android:height="100dp" android:width="100dp" /> android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="@string/makeMove" android:text="@string/empty" android:height="100dp" android:width="100dp" /> android:id="@+id/button6" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="@string/makeMove" android:text="@string/empty" android:height="100dp" android:width="100dp"/> </TableRow> <TableRow> android:id="@+id/button7" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="@string/makeMove" android:text="@string/empty" android:height="100dp" android:width="100dp" /> android:id="@+id/button8" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="@string/makeMove" android:text="@string/empty" android:height="100dp" android:width="100dp" /> android:id="@+id/button9" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="@string/makeMove" android:text="@string/empty" android:height="100dp" android:width="100dp"/> </TableRow> </TableLayout> </RelativeLayout>
private void checkResult() { boolean empty = false; AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this); for (int i = 0; i != 3; ++i) { for (int j = 0; j != 3; ++j) { if (table[i][j]==0) { empty = true; break; } } } if (!empty) { dlgAlert.setMessage("Draw!"); dlgAlert.setTitle("Draw"); dlgAlert.setCancelable(true); dlgAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); } }); dlgAlert.create().show(); } //check horizontal lines for (int i = 0; i != 3; ++i) { if (table[i][0] == 1 && table[i][1] == 1 && table[i][2] == 1) { dlgAlert.setMessage("O Player wins!"); dlgAlert.setTitle("congratulations"); dlgAlert.setCancelable(true); dlgAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); } }); dlgAlert.create().show(); } if (table[i][0] == 2 && table[i][1] == 2 && table[i][2] == 2) { dlgAlert.setMessage("X Player wins!"); dlgAlert.setTitle("congratulations"); dlgAlert.setCancelable(true); dlgAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); } }); dlgAlert.create().show(); } } //check vertical lines for (int i = 0; i != 3; ++i) { if (table[0][i] == 1 && table[1][i] == 1 && table[2][i] == 1) { dlgAlert.setMessage("O Player wins!"); dlgAlert.setTitle("congratulations"); dlgAlert.setCancelable(true); dlgAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); } }); dlgAlert.create().show(); } if (table[0][i] == 2 && table[1][i] == 2 && table[2][i] == 2) { dlgAlert.setMessage("X Player wins!"); dlgAlert.setTitle("congratulations"); dlgAlert.setCancelable(true); dlgAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); } }); dlgAlert.create().show(); } } //check diagonals if (table[0][0] == 1 && table[1][1] == 1 && table[2][2] == 1) { dlgAlert.setMessage("O Player wins!"); dlgAlert.setTitle("congratulations"); dlgAlert.setCancelable(true); dlgAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); } }); dlgAlert.create().show(); } if (table[0][0] == 2 && table[1][1] == 2 && table[2][2] == 2) { dlgAlert.setMessage("X Player wins!"); dlgAlert.setTitle("congratulations"); dlgAlert.setCancelable(true); dlgAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); } }); dlgAlert.create().show(); } if (table[0][2] == 1 && table[1][1] == 1 && table[2][0] == 1) { dlgAlert.setMessage("O Player wins!"); dlgAlert.setTitle("congratulations"); dlgAlert.setCancelable(true); dlgAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); } }); dlgAlert.create().show(); } if (table[0][2] == 2 && table[1][1] == 2 && table[2][0] == 2) { dlgAlert.setMessage("X Player wins!"); dlgAlert.setTitle("congratulations"); dlgAlert.setCancelable(true); dlgAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); } }); dlgAlert.create().show(); } }
doesn't work. tried playing it. x goes first then nothing happens. 0 doesn't move. click anywhere and it says space is not empty.