I had created a Tic Tac Toe game,which created a black and red image user have to create three pair of sets,two user can play,any one whoever made it first will win .I created a Play again button for restarting the game again and again ,but after clicking that button, i am getting out of the app. Can you guys help me where i am going wrong .Below i am giving my code.
MainActivity.java
package com.example.tictac;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
//0:cross,1:red,2:empty
int[] gameState = {2,2,2,2,2,2,2,2,2};
int[][] winningPositions = {{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}};
int activePlayer =0;
boolean gameActive = true;
public void dropIn(View view){
ImageView counter = (ImageView) view;
int tappedCounter = Integer.parseInt(counter.getTag().toString());
if(gameState[tappedCounter] == 2 && gameActive) {
gameState[tappedCounter] = activePlayer;
counter.setTranslationY(-1500);
if (activePlayer == 0) {
counter.setImageResource(R.drawable.cross);
activePlayer = 1;
} else {
counter.setImageResource(R.drawable.redd);
activePlayer = 0;
}
counter.animate().translationYBy(1500).rotation(3600).setDuration(300);
for (int[] winningPositions : winningPositions) {
if (gameState[winningPositions[0]] == gameState[winningPositions[1]] && gameState[winningPositions[1]] == gameState[winningPositions[2]] && gameState[winningPositions[0]] != 2) {
//someone win
gameActive = false;
String winner = "";
if (activePlayer == 1) {
winner = "Black";
} else {
winner = "Red";
}
Button playAgainButton = (Button) findViewById(R.id.playAgainButton);
TextView winnerTextView = (TextView) findViewById(R.id.winnerTextView);
winnerTextView.setText(winner + " has won");
playAgainButton.setVisibility(View.VISIBLE);
winnerTextView.setVisibility(View.VISIBLE);
}
}
}
}
public void okayPlay(View view){
Button playAgainButton = (Button) findViewById(R.id.playAgainButton);
TextView winnerTextView = (TextView) findViewById(R.id.winnerTextView);
playAgainButton.setVisibility(View.INVISIBLE);
winnerTextView.setVisibility(View.INVISIBLE);
GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout);
for(int i=0; i<gridLayout.getChildCount() ;i++){
ImageView counter =(ImageView) gridLayout.getChildAt(i);
counter.setImageDrawable(null);
}
for(int i=0;i<gameState.length;i++){
gameState[i]=2;
}
activePlayer =0;
gameActive = true;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Content.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<androidx.gridlayout.widget.GridLayout
android:id="#+id/gridLayout"
android:layout_width="368dp"
android:layout_height="368dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:background="#drawable/bd"
app:columnCount="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.296"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498"
app:rowCount="3">
<ImageView
android:id="#+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:layout_marginLeft="17dp"
android:contentDescription="TODO"
android:onClick="dropIn"
android:tag="0"
app:layout_column="0"
app:layout_row="0" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:layout_marginLeft="10dp"
android:onClick="dropIn"
android:tag="1"
app:layout_column="1"
app:layout_row="0"
tools:ignore="SpeakableTextPresentCheck" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:layout_marginLeft="10dp"
android:onClick="dropIn"
android:tag="2"
app:layout_column="2"
app:layout_row="0"
tools:ignore="SpeakableTextPresentCheck" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:layout_marginLeft="17dp"
android:onClick="dropIn"
android:tag="3"
app:layout_column="0"
app:layout_row="1"
tools:ignore="SpeakableTextPresentCheck" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:layout_marginLeft="10dp"
android:onClick="dropIn"
android:tag="4"
app:layout_column="1"
app:layout_row="1"
tools:ignore="SpeakableTextPresentCheck" />
<ImageView
android:id="#+id/imageView6"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:onClick="dropIn"
android:tag="5"
app:layout_column="2"
app:layout_row="1"
tools:ignore="SpeakableTextPresentCheck" />
<ImageView
android:id="#+id/imageView7"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:layout_marginLeft="17dp"
android:onClick="dropIn"
android:tag="6"
app:layout_column="0"
app:layout_row="2"
tools:ignore="SpeakableTextPresentCheck" />
<ImageView
android:id="#+id/imageView8"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:layout_marginLeft="10dp"
android:onClick="dropIn"
android:tag="7"
app:layout_column="1"
app:layout_row="2"
tools:ignore="SpeakableTextPresentCheck" />
<ImageView
android:id="#+id/imageView9"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:layout_marginLeft="10dp"
android:onClick="dropIn"
android:tag="8"
app:layout_column="2"
app:layout_row="2"
tools:ignore="SpeakableTextPresentCheck" />
</androidx.gridlayout.widget.GridLayout>
<TextView
android:id="#+id/winnerTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="176dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="177dp"
android:text="TextView"
android:textSize="20sp"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="#+id/gridLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.349" />
<Button
android:id="#+id/playAgainButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="164dp"
android:layout_marginEnd="153dp"
android:onClick="okayPlay"
android:text="#string/play_again"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/winnerTextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
Your app is crashing because in your okayPlay(View view) method you're trying to cast
androidx.gridlayout.widget.GridLayout
to
android.widget.GridLayout
In your Activity, change your android.widget.GridLayout import to
androidx.gridlayout.widget.GridLayout
Related
I have added onClickListener to buttons which should add the button text to TextView. But on clicking button, text of previously clicked button is getting added to text.
I tried creating other app it worked, it was adding text of the clicked button correctly.
I am new to Android.
Trying to build calculator here
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/ptButton"
style="?attr/borderlessButtonStyle"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:background="#null"
android:fadingEdge="horizontal|vertical"
android:text="."
android:textSize="30sp"
app:layout_constraintStart_toEndOf="#+id/button0"
app:layout_constraintTop_toBottomOf="#+id/button2" />
<Button
android:id="#+id/equalButton"
style="?attr/borderlessButtonStyle"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:background="#null"
android:fadingEdge="horizontal|vertical"
android:text="="
android:textSize="30sp"
app:layout_constraintStart_toEndOf="#+id/ptButton"
app:layout_constraintTop_toBottomOf="#+id/button3" />
<Button
android:id="#+id/button0"
style="?attr/borderlessButtonStyle"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:background="#null"
android:fadingEdge="horizontal|vertical"
android:text="0"
android:textSize="30sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button1" />
<TextView
android:id="#+id/answerField"
android:layout_width="400dp"
android:layout_height="127dp"
android:cursorVisible="true"
android:duplicateParentState="false"
android:editable="false"
android:gravity="right"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.454"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.099" />
<Button
android:id="#+id/button7"
style="?attr/borderlessButtonStyle"
android:layout_width="90dp"
android:layout_height="90dp"
android:background="#null"
android:fadingEdge="horizontal|vertical"
android:text="7"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.02"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.39" />
<Button
android:id="#+id/button1"
style="?attr/borderlessButtonStyle"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:background="#null"
android:fadingEdge="horizontal|vertical"
android:text="1"
android:textSize="30sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button4" />
<Button
android:id="#+id/button4"
style="?attr/borderlessButtonStyle"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:background="#null"
android:fadingEdge="horizontal|vertical"
android:text="4"
android:textSize="30sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button7" />
<Button
android:id="#+id/button8"
style="?attr/borderlessButtonStyle"
android:layout_width="90dp"
android:layout_height="90dp"
android:background="#null"
android:fadingEdge="horizontal|vertical"
android:text="8"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.35"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.39" />
<Button
android:id="#+id/button9"
style="?attr/borderlessButtonStyle"
android:layout_width="90dp"
android:layout_height="90dp"
android:background="#null"
android:fadingEdge="horizontal|vertical"
android:text="9"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.685"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.39" />
<Button
android:id="#+id/button6"
style="?attr/borderlessButtonStyle"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:background="#null"
android:fadingEdge="horizontal|vertical"
android:text="6"
android:textSize="30sp"
app:layout_constraintStart_toEndOf="#+id/button5"
app:layout_constraintTop_toBottomOf="#+id/button9" />
<Button
android:id="#+id/button3"
style="?attr/borderlessButtonStyle"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:background="#null"
android:fadingEdge="horizontal|vertical"
android:text="3"
android:textSize="30sp"
app:layout_constraintStart_toEndOf="#+id/button2"
app:layout_constraintTop_toBottomOf="#+id/button6" />
<Button
android:id="#+id/button2"
style="?attr/borderlessButtonStyle"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:background="#null"
android:fadingEdge="horizontal|vertical"
android:text="2"
android:textSize="30sp"
app:layout_constraintStart_toEndOf="#+id/button1"
app:layout_constraintTop_toBottomOf="#+id/button5" />
<Button
android:id="#+id/button5"
style="?attr/borderlessButtonStyle"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:background="#null"
android:fadingEdge="horizontal|vertical"
android:text="5"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/button4"
app:layout_constraintTop_toBottomOf="#+id/button8"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="#+id/addButton"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:background="#android:color/transparent"
android:text="+"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/clearButton" />
<Button
android:id="#+id/subButton"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:background="#android:color/transparent"
android:text="*"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/addButton"
tools:text="-" />
<Button
android:id="#+id/multButton"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:background="#android:color/transparent"
android:text="*"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/subButton" />
<Button
android:id="#+id/divButton"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:background="#android:color/transparent"
android:text="/"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/multButton" />
<Button
android:id="#+id/clearButton"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="#android:color/transparent"
android:text="C"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.953"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.39" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
I have added actionListeners to all buttons.
package com.advanced.calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, addButton, subButton, multButton, divButton, equalButton, ptButton, clearButton;
TextView answerField;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button0 = (Button) findViewById(R.id.button0);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button7 = (Button) findViewById(R.id.button7);
button8 = (Button) findViewById(R.id.button8);
button9 = (Button) findViewById(R.id.button9);
addButton = (Button) findViewById(R.id.addButton);
subButton = (Button) findViewById(R.id.subButton);
multButton = (Button) findViewById(R.id.multButton);
divButton = (Button) findViewById(R.id.divButton);
equalButton = (Button) findViewById(R.id.equalButton);
ptButton = (Button) findViewById(R.id.ptButton);
clearButton = (Button) findViewById(R.id.clearButton);
answerField = (TextView) findViewById(R.id.answerField);
clearButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
answerField.setText("");
}
});
button0.setOnClickListener(this);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
button6.setOnClickListener(this);
button7.setOnClickListener(this);
button8.setOnClickListener(this);
button9.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Button b = (Button) v;
answerField.append(b.getText());
}
}
Please help me. ;)
for best approach try using MVVM architecture with data-binding, its lot quicker and easy to implement, just a suggestion ;P...
so your problem is when u calling your onClick method, you are not verifying the view with view ID, causing this issue.
try this...
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
Toast.makeText(this, "Button 1 clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
Toast.makeText(this, "Button 2 clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.button3:
Toast.makeText(this, "Button 3 clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.button4:
Toast.makeText(this, "Button 4 clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.button5:
Toast.makeText(this, "Button 5 clicked", Toast.LENGTH_SHORT).show();
break;
}
}
I built an X and O game on Android Studio when I run the app on the emulator it operates normally but when I run it on the phone it is very slow, I built before a similar app called Tick tac toe it has the same code and logic but different images only, but it runs fast on both my phone and the emulator, I suspected that the size of the X and O images is huge so the game runs slow but when I checked the sizes it has nearly similar sizes with the Tick tac toe I don't know why the game is slow even it is very simple please help.
the code:
Firstly the Tick tac toe:
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<GridLayout
android:id="#+id/gridLayout"
android:layout_width="368dp"
android:layout_height="360dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#drawable/board"
android:columnCount="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:rowCount="3">
<ImageView
android:id="#+id/imageView"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:onClick="dropIn"
android:tag="0"
app:layout_column="0"
app:layout_row="0" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:onClick="dropIn"
android:tag="1"
app:layout_column="1"
app:layout_row="0" />
<ImageView
android:id="#+id/imageView6"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:onClick="dropIn"
android:tag="2"
app:layout_column="2"
app:layout_row="0" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_margin="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="25dp"
android:onClick="dropIn"
android:tag="3"
app:layout_column="0"
app:layout_row="1" />
<ImageView
android:id="#+id/imageView7"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="10dp"
android:layout_marginTop="25dp"
android:onClick="dropIn"
android:tag="4"
app:layout_column="1"
app:layout_row="1" />
<ImageView
android:id="#+id/imageView8"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="10dp"
android:layout_marginTop="25dp"
android:onClick="dropIn"
android:tag="5"
app:layout_column="2"
app:layout_row="1" />
<ImageView
android:id="#+id/imageView9"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="25dp"
android:onClick="dropIn"
android:tag="6"
app:layout_column="0"
app:layout_row="2" />
<ImageView
android:id="#+id/imageView10"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="10dp"
android:layout_marginTop="25dp"
android:onClick="dropIn"
android:tag="7"
app:layout_column="1"
app:layout_row="2" />
<ImageView
android:id="#+id/imageView11"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="10dp"
android:layout_marginTop="25dp"
android:onClick="dropIn"
android:tag="8"
app:layout_column="2"
app:layout_row="2" />
</GridLayout>
<LinearLayout
android:id="#+id/playAgainLayout"
android:layout_width="189dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="100dp"
android:layout_marginRight="8dp"
android:layout_marginStart="100dp"
android:layout_marginTop="8dp"
android:background="#46d65e"
android:orientation="vertical"
android:padding="30dp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/gridLayout"
app:layout_constraintHorizontal_bias="0.011"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/winnerMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TextView"
android:textSize="30sp" />
<Button
android:id="#+id/playAgainButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="playAgain"
android:text="Play Again" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
JAVA
package com.k_pc.tick_tac_toe;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
// 0 = yellow, 1 = red;
int activePlayer = 0;
// 2 means unplayed
int [] gameState= {2, 2, 2, 2, 2, 2, 2, 2, 2};
int [] [] winningPositions = {{0,1,2}, {3,4,5}, {6,7,8}, {0,3,6}, {1,4,7}, {2,5,8}, {0,4,8}, {2,4,6}};
public void dropIn (View view){
ImageView counter = (ImageView) view;
System.out.println(counter.getTag().toString());
int tappedCounter = Integer.parseInt(counter.getTag().toString());
if (gameState[tappedCounter] == 2) {
gameState[tappedCounter] = activePlayer;
counter.setTranslationY(-1000f);
if (activePlayer == 0) {
counter.setImageResource(R.drawable.yellow);
activePlayer = 1;
} else {
counter.setImageResource(R.drawable.red);
activePlayer = 0;
}
counter.animate().translationYBy(1000f).rotation(360f).setDuration(500);
for (int [] winningPosition : winningPositions){
if (gameState[winningPosition[0]] == gameState[winningPosition[1]] &&
gameState[winningPosition[1]] == gameState[winningPosition[2]] &&
gameState[winningPosition[0]] != 2){
//Someone has won
String winner = "Red";
if (gameState[winningPosition[0]] == 0){
winner = "Yellow";
}
TextView winnerMeassage = (TextView) findViewById(R.id.winnerMessage);
winnerMeassage.setText(winner + " has won!");
LinearLayout layout = (LinearLayout) findViewById(R.id.playAgainLayout);
layout.setVisibility(View.VISIBLE);
}
}
}
}
public void playAgain (View view){
LinearLayout layout = (LinearLayout) findViewById(R.id.playAgainLayout);
layout.setVisibility(View.INVISIBLE);
// 0 = yellow, 1 = red;
activePlayer = 0;
// 2 means unplayed
for (int i = 0 ; i < gameState.length ; i++){
gameState[i] = 2;
}
// setting all the images source to nothing
GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout);
for(int i = 0 ; i<gridLayout.getChildCount() ; i++){
((ImageView) gridLayout.getChildAt(i)).setImageResource(0);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Images of Tick tac toe:
1. https://imgur.com/f2UW3ZO
2. https://imgur.com/lxSGOh9
3. https://imgur.com/aUCNtOx
Secondly the X and O:
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<GridLayout
android:id="#+id/gridLayout"
android:layout_width="360dp"
android:layout_height="360dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#drawable/board"
android:columnCount="3"
android:rowCount="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/imageView"
android:layout_width="115dp"
android:layout_height="115dp"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_row="0"
android:onClick="dropIn"
android:tag="0" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="115dp"
android:layout_height="115dp"
android:layout_column="1"
android:layout_row="0"
android:onClick="dropIn"
android:tag="1" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="115dp"
android:layout_height="115dp"
android:layout_column="2"
android:layout_row="0"
android:onClick="dropIn"
android:tag="2" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="115dp"
android:layout_height="115dp"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_row="1"
android:onClick="dropIn"
android:tag="3" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="115dp"
android:layout_height="115dp"
android:layout_column="1"
android:layout_marginTop="10dp"
android:layout_row="1"
android:onClick="dropIn"
android:tag="4" />
<ImageView
android:id="#+id/imageView6"
android:layout_width="115dp"
android:layout_height="115dp"
android:layout_column="2"
android:layout_marginTop="10dp"
android:layout_row="1"
android:onClick="dropIn"
android:tag="5"/>
<ImageView
android:id="#+id/imageView7"
android:layout_width="115dp"
android:layout_height="115dp"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_row="2"
android:onClick="dropIn"
android:tag="6"/>
<ImageView
android:id="#+id/imageView8"
android:layout_width="115dp"
android:layout_height="115dp"
android:layout_column="1"
android:layout_row="2"
android:onClick="dropIn"
android:tag="7"/>
<ImageView
android:id="#+id/imageView9"
android:layout_width="115dp"
android:layout_height="115dp"
android:layout_column="2"
android:layout_row="2"
android:onClick="dropIn"
android:tag="8"/>
</GridLayout>
<LinearLayout
android:id="#+id/playAgainLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#4be433"
android:orientation="vertical"
android:padding="30dp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/winnerMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="30sp" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="playAgain"
android:text="PLAY AGAIN" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
JAVA
package com.k_pc.xando;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
// 0 = x, 1 = o
int activePlayer = 0;
// 2 means unplayed
int [] gameState ={2, 2, 2, 2, 2, 2, 2, 2, 2};
int [] [] winningPositions = {{0,1,2}, {3,4,5}, {6,7,8}, {0,3,6}, {1,4,7}, {2,5,8}, {0,4,8}, {2,4,6}};
public void dropIn (View view){
ImageView counter = (ImageView) view;
System.out.println("Tag: "+counter.getTag().toString());
counter.setTranslationY(-1000f);
int tappedCounter = Integer.parseInt(counter.getTag().toString());
if (gameState[tappedCounter] == 2) {
gameState[tappedCounter] = activePlayer;
if (activePlayer == 0) {
counter.setImageResource(R.drawable.x);
activePlayer = 1;
} else {
counter.setImageResource(R.drawable.o);
activePlayer = 0;
}
}
counter.animate().translationYBy(1000f).rotation(720).setDuration(500);
for (int [] winningPosition : winningPositions){
if (gameState[winningPosition[0]] == gameState[winningPosition[1]] &&
gameState[winningPosition[1]] == gameState[winningPosition[2]] &&
gameState[winningPosition[0]] != 2){
//Someone has won
String winner = "O";
if (gameState[winningPosition[0]] == 0){
winner = "X";
}
TextView winnerMeassage = (TextView) findViewById(R.id.winnerMessage);
winnerMeassage.setText(winner + " has won!");
LinearLayout layout = (LinearLayout) findViewById(R.id.playAgainLayout);
layout.setVisibility(View.VISIBLE);
}
}
int played = 0;
for (int i = 0; i < gameState.length; i++){
if (gameState[i] != 2){
played++;
}
}
if(played == gameState.length){
TextView winnerMeassage = (TextView) findViewById(R.id.winnerMessage);
winnerMeassage.setText("No one has won!");
LinearLayout layout = (LinearLayout) findViewById(R.id.playAgainLayout);
layout.setVisibility(View.VISIBLE);
}
}
public void playAgain (View view){
LinearLayout layout = (LinearLayout) findViewById(R.id.playAgainLayout);
layout.setVisibility(View.INVISIBLE);
// 0 = yellow, 1 = red;
activePlayer = 0;
// 2 means unplayed
for (int i = 0 ; i < gameState.length ; i++){
gameState[i] = 2;
}
// setting all the images source to nothing
GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout);
for(int i = 0 ; i<gridLayout.getChildCount() ; i++){
((ImageView) gridLayout.getChildAt(i)).setImageResource(0);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
The images for X and O:
1. https://imgur.com/f2UW3ZO
2. https://imgur.com/EqQl6yk
3. https://imgur.com/LzsT0wG
I don't think the code will help because both have the same code but one only is faster than the other, the problem may be in the images.
Here is a video showing the two apps and how slow the X and O compared to Tick tac toe.
https://vimeo.com/291295377
Hi friends I am making Tic Tac Toe app.
I want to make it computer vs Player mode currently it is in a two player mode.
getting confused in how to set the input in Gridview layout as a ImageView.
int activePlayer = 0;
boolean gameIsActive = true;
// 2 means unplayed 0 means computer and 1 means Player moves
int[] gameState = {2, 2, 2, 2, 2, 2, 2, 2, 2};
public void dropIn(View view) {
ImageView counter = (ImageView) view;
int tappedCounter = Integer.parseInt(counter.getTag().toString());
if (gameState[tappedCounter] == 2 && gameIsActive) {
gameState[tappedCounter] = activePlayer;
counter.setTranslationY(-1000f);
if (activePlayer == 0) {
counter.setImageResource(R.drawable.yellow);
activePlayer = 1;
}
else {
counter.setImageResource(R.drawable.red);
activePlayer = 0;
}
counter.animate().translationYBy(1000f).rotation(360).setDuration(300);
}
XML FILE
<GridLayout
android:layout_width="match_parent"
android:layout_height="360dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:columnCount="3"
android:rowCount="3"
android:background="#drawable/board"
android:layout_alignParentEnd="true"
android:id="#+id/gridLayout">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/imageView"
android:layout_row="0"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:onClick="dropIn"
android:tag="0" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/imageView2"
android:layout_row="0"
android:layout_column="1"
android:layout_marginTop="10dp"
android:layout_marginLeft="25dp"
android:onClick="dropIn"
android:tag="1" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/imageView3"
android:layout_row="0"
android:layout_column="2"
android:layout_marginTop="10dp"
android:layout_marginLeft="25dp"
android:onClick="dropIn"
android:tag="2" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/imageView4"
android:layout_row="1"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_marginTop="24dp"
android:onClick="dropIn"
android:tag="3" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/imageView5"
android:layout_row="1"
android:layout_column="1"
android:layout_marginLeft="25dp"
android:layout_marginTop="24dp"
android:onClick="dropIn"
android:tag="4" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/imageView6"
android:layout_row="1"
android:layout_column="2"
android:layout_marginLeft="25dp"
android:layout_marginTop="24dp"
android:onClick="dropIn"
android:tag="5" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/imageView7"
android:layout_row="2"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_marginTop="30dp"
android:onClick="dropIn"
android:tag="6" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/imageView8"
android:layout_row="2"
android:layout_column="1"
android:layout_marginLeft="25dp"
android:layout_marginTop="30dp"
android:onClick="dropIn"
android:tag="7" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/imageView9"
android:layout_row="2"
android:layout_column="2"
android:layout_marginLeft="25dp"
android:layout_marginTop="30dp"
android:onClick="dropIn"
android:tag="8" />
</GridLayout>
I want to chose activeplayer=0 image randomly?
Define an int array of DrawableRes and access using random number. For random number generation use the answer of user "I need a job"
#DrawableRes int [] activePlayerRes = new int [] {R.drawable.yellow,....};
After all you have use change your call like
counter.setImageResource(activePlayerRes[randomRes]); // make sure your random should return in range of array you defined.
It seems I am not able to get the startingPlayerLayout to the front of other Views. I have tried multiple solutions, the last one being the code below, which as a result keeps the LinearLayout visible but in the background...
(It's my very first question on Stack Overflow, please excuse any mistakes)
Thanks to everyone
public class MainActivity extends AppCompatActivity {
boolean gameFinished = false;
// Player 1 = Black, Player 2 = Red
int playerActive;
int[] gameState = {0, 0, 0, 0, 0, 0, 0, 0, 0};
int[][] winningPositions = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6}};
TextView gameInfoField;
Button playAgainButton;
LinearLayout startingPlayerLayout;
ConstraintLayout baseLayout;
// The actual gameplay follows:
public void player1Starts(View view) {
playerActive = 1;
startingPlayerLayout.setVisibility(View.INVISIBLE);
}
public void player2Starts(View view) {
playerActive = 2;
startingPlayerLayout.setVisibility(View.INVISIBLE);
}
public void playCounter(View view) {
ImageView counter = (ImageView) view;
int squarePlayed = Integer.parseInt(counter.getTag().toString());
gameInfoField = (TextView) findViewById(R.id.gameInfoField);
if (!gameFinished) {
if (gameState[squarePlayed] == 0) {
counter.setTranslationY(-1000f);
if (playerActive == 1) {
counter.setImageResource(R.drawable.token_black);
gameState[squarePlayed] = 1;
playerActive = 2;
gameInfoField.setText("Tocca al ROSSO");
} else {
counter.setImageResource(R.drawable.token_red);
gameState[squarePlayed] = 2;
playerActive = 1;
gameInfoField.setText("Tocca al NERO");
}
counter.animate().translationYBy(1000f);
} else if (gameState[squarePlayed] == 0 && !gameFinished) {
Toast.makeText(getApplicationContext(), "La casella è già occupata", Toast.LENGTH_SHORT).show();
}
// Need to check if there's a winner, or if it's a draw:
for (int[] winningPosition : winningPositions) {
playAgainButton = (Button) findViewById(R.id.playAgainButton);
if (gameState[winningPosition[0]] == gameState[winningPosition[1]] &&
gameState[winningPosition[1]] == gameState[winningPosition[2]] &&
gameState[winningPosition[0]] != 0) {
gameFinished = true;
if (gameState[winningPosition[0]] == 1) {
gameInfoField.setText("IL NERO VINCE!");
} else {
gameInfoField.setText("IL ROSSO VINCE!");
}
gameFinished = true;
playAgainButton.setVisibility(View.VISIBLE);
}
boolean fullBoard = true;
for (int square : gameState) {
if (square == 0) {
fullBoard = false;
}
}
if (fullBoard && !gameFinished) {
gameInfoField.setText("Pareggio");
playAgainButton.setVisibility(View.VISIBLE);
}
}
} else {
Toast.makeText(getApplicationContext(), "Abbiamo già un vincitore!", Toast.LENGTH_SHORT).show();
}
}
// And finally, we need to be able to play again if we want to:
public void playAgain(View view) {
gameFinished = false;
for (int i = 0; i < gameState.length; i++) {
gameState[i] = 0;
}
GridLayout board = (GridLayout) findViewById(R.id.board);
for (int i = 0; i < board.getChildCount(); i++) {
((ImageView) board.getChildAt(i)).setImageResource(0);
}
playAgainButton.setVisibility(View.INVISIBLE);
baseLayout.bringChildToFront(startingPlayerLayout);
startingPlayerLayout.setVisibility(View.VISIBLE);
if (playerActive == 1) {
gameInfoField.setText("Tocca al NERO");
}
else {
gameInfoField.setText("Tocca al ROSSO");
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
baseLayout = (ConstraintLayout) findViewById(R.id.baseLayout);
startingPlayerLayout = (LinearLayout) findViewById(R.id.startingPlayerLayout);
baseLayout.bringChildToFront(startingPlayerLayout);
startingPlayerLayout.setVisibility(View.VISIBLE);
}
}
And here is the XML file (I tried to move the LinearLayout at the end of it but the problem remains the same):
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/baseLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_bright"
tools:context="com.williamzannoni.tris.MainActivity">
<LinearLayout
android:id="#+id/startingPlayerLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_orange_light"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/startingPlayerText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CHI COMINCIA?"
android:textSize="25sp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="5dp" />
<Button
android:id="#+id/startingBlackButton"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:background="?android:attr/colorButtonNormal"
android:onClick="player1Starts"
android:text="NERO" />
<Button
android:id="#+id/startingRedButton"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:onClick="player2Starts"
android:text="ROSSO" />
</LinearLayout>
<GridLayout
android:id="#+id/board"
android:layout_width="368dp"
android:layout_height="368dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#drawable/board_grey"
android:columnCount="3"
android:elevation="1dp"
android:rowCount="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.496">
<ImageView
android:id="#+id/imageView4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="12dp"
android:layout_marginTop="15dp"
android:onClick="playCounter"
android:tag="0" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="22dp"
android:layout_marginTop="15dp"
android:onClick="playCounter"
android:tag="1" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="22dp"
android:layout_marginTop="15dp"
android:onClick="playCounter"
android:tag="2" />
<ImageView
android:id="#+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_column="0"
android:layout_marginLeft="12dp"
android:layout_marginTop="22dp"
android:layout_row="1"
android:onClick="playCounter"
android:tag="3" />
<ImageView
android:id="#+id/imageView7"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="22dp"
android:layout_marginTop="22dp"
android:onClick="playCounter"
android:tag="4" />
<ImageView
android:id="#+id/imageView6"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="22dp"
android:layout_marginTop="22dp"
android:onClick="playCounter"
android:tag="5" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_column="0"
android:layout_marginLeft="12dp"
android:layout_marginTop="22dp"
android:layout_row="2"
android:onClick="playCounter"
android:tag="6" />
<ImageView
android:id="#+id/imageView9"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="22dp"
android:layout_marginTop="22dp"
android:onClick="playCounter"
android:tag="7" />
<ImageView
android:id="#+id/imageView8"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_column="2"
android:layout_marginLeft="22dp"
android:layout_marginTop="22dp"
android:layout_row="2"
android:onClick="playCounter"
android:tag="8" />
</GridLayout>
<TextView
android:id="#+id/gameInfoField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#android:color/holo_blue_bright"
android:padding="10dp"
android:text="Tocca al NERO"
android:textSize="25sp"
app:layout_constraintBottom_toTopOf="#+id/board"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.727" />
<Button
android:id="#+id/playAgainButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:background="#android:color/holo_blue_dark"
android:onClick="playAgain"
android:padding="10dp"
android:text="GIOCA ANCORA"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>
The order of the views declared in XML is important here. The views first declared in R.layout.activity_main will be drawn before the views near the bottom of the XML file.
This means you may have to re-order the views in your layout file in order to get the design you are looking for.
P.s. if you post your layout file we can try to help further.
Now i encountered problem while debugging my code,that the Buttons in my application is not responding to click action.I kept break point on Click view method but on click view method is not responding.
What i am doing in on Click method is, if i clicked the first button then swapping the value with empty button value.But on click is not responding.
Here is my full code to check.
public class Play extends Activity implements OnClickListener
{
private ArrayList<Integer> m_buildButtonsIDs;
private ArrayList<Integer> m_gameButtonsIDs;
public static Character UserWords;
public static Character RemainingWords;
public static Character OpenCard;
static ArrayList<Character> ShuffledCards = new ArrayList<Character>();
static ArrayList<Character> UserBuildWords = new ArrayList<Character>();
static ArrayList<Character> RemainingBuildWords = new ArrayList<Character>();
static ArrayList<Character> RemainingBuildWordsAfterShowCard = new ArrayList<Character>();
static ArrayList<Character> DroppedCards = new ArrayList<Character>();
static Stack<Character>UserDroppedCards = new Stack<Character>();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.play);
InitializeBoard();
}
public void InitializeBoard()
{
m_buildButtonsIDs = new ArrayList<Integer>();
m_buildButtonsIDs.add(R.id.Button1);
m_buildButtonsIDs.add(R.id.Button2);
m_buildButtonsIDs.add(R.id.Button3);
m_buildButtonsIDs.add(R.id.Button4);
m_buildButtonsIDs.add(R.id.Button5);
m_buildButtonsIDs.add(R.id.Button6);
m_buildButtonsIDs.add(R.id.Button7);
m_buildButtonsIDs.add(R.id.Button8);
m_buildButtonsIDs.add(R.id.Button9);
m_gameButtonsIDs = new ArrayList<Integer>();
m_gameButtonsIDs.add(R.id.pickButton);
m_gameButtonsIDs.add(R.id.showButton);
m_gameButtonsIDs.add(R.id.emptyButton);
String random = RandomAlphabetGenerator.Random();
for(int i = 0;i<random.length();i++)
{
char randomcards = random.charAt(i);
ShuffledCards.add(randomcards);
}
for(int i = 0;i < 9;i++)
{
UserWords = ShuffledCards.get(i);
UserBuildWords.add(UserWords);
if(i == 8)
{
for(int j = 9;j < 52;j++)
{
RemainingWords = ShuffledCards.get(j);
RemainingBuildWords.add(RemainingWords);
}
}
}
OpenCard = RemainingBuildWords.get(0);
DroppedCards.add(OpenCard);
RemainingBuildWords.remove(0);
RemainingBuildWordsAfterShowCard.addAll(RemainingBuildWords);
FillUserBuildButtons(ShuffledCards);
StackingDroppedButtons(DroppedCards);
StackingPickButtons(RemainingBuildWordsAfterShowCard);
}
private void FillUserBuildButtons(ArrayList<Character> shuffledCards)
{
for (int i=0 ; i<m_buildButtonsIDs.size() ; i++)
{
Button BuildButton = (Button)findViewById(m_buildButtonsIDs.get(i));
BuildButton.setText(UserBuildWords.get(i).toString());
}
}
private void StackingDroppedButtons(ArrayList<Character> droppedCards)
{
Button ShowButton = (Button)findViewById(m_gameButtonsIDs.get(1));
ShowButton.setText(DroppedCards.get(0).toString());
}
private void StackingPickButtons(ArrayList<Character> remainingBuildWordsAfterShowCard)
{
Button ShowButton = (Button)findViewById(m_gameButtonsIDs.get(0));
ShowButton.setText(RemainingBuildWordsAfterShowCard.get(0).toString());
}
public void onClick(View v)
{
CharSequence text;
switch(v.getId())
{
case R.id.Button1:
Button FirstButton = (Button)findViewById(m_buildButtonsIDs.get(0));
text = FirstButton.getText().toString();
FirstButton.setText(SwapValue(text));
break;
case R.id.emptyButton:
break;
}
}
private CharSequence SwapValue(CharSequence k)
{
CharSequence empty = null;
Button EmptyButton = (Button)findViewById(m_buildButtonsIDs.get(0));
empty = EmptyButton.getText().toString();
EmptyButton.setText(k);
return empty;
}
}
Here is my xml code for checking:
<TableLayout
android:id="#+id/MainLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginTop="50dp"
android:layout_marginLeft="115dp">
<LinearLayout
android:layout_width="40dp"
android:layout_height="40dp" >
<Button
android:id="#+id/Button1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp" />
<Button
android:id="#+id/Button2"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp" />
<Button
android:id="#+id/Button3"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp" />
<Button
android:id="#+id/Dummy"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:clickable="false"
android:visibility="invisible" />
<Button
android:id="#+id/Button4"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp" />
<Button
android:id="#+id/Button5"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp" />
<Button
android:id="#+id/Button6"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp" />
</LinearLayout>
<LinearLayout
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="30dp">
<Button
android:id="#+id/Dummy2"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:clickable="false"
android:visibility="invisible" />
<Button
android:id="#+id/Dummy3"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:clickable="false"
android:visibility="invisible" />
<Button
android:id="#+id/Button7"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp" />
<Button
android:id="#+id/Button8"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp" />
<Button
android:id="#+id/Button9"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp"/>
<Button
android:id="#+id/Dummy4"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:clickable="false"
android:visibility="invisible" />
<Button
android:id="#+id/Dummy5"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:clickable="false"
android:visibility="invisible" />
</LinearLayout>
<LinearLayout
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="20dp">
<Button
android:id="#+id/pickButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp"/>
<Button
android:id="#+id/showButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp"/>
<Button
android:id="#+id/emptyButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/empty"
android:textSize="15dp" />
<Button
android:id="#+id/emptyButton1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text=""
android:textSize="15dp" />
<Button
android:id="#+id/dropButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/drop"
android:textSize="15dp"/>
<Button
android:id="#+id/declareButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/declare"
android:textSize="15dp"/>
</LinearLayout>
</TableLayout>
you should add an onClickListener to your Button :
btn.setOnClickListener(Play.this);