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.
Related
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
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
In the building, tic-tac-toe grid layout occupies the upper layer and Linearlayout occupies the lower layer just below the grid layout. where the Linear layout should appear if any player wins the match. In linear layout the player who wins and a button to set the game to the starting position. So initially I set it to invisible when the game ends linear layout comes back side of grid layout. How can I make the Linear Layout on top of grid layout?
MainActivity.java
package com.example.achyu.tictactao;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
//0=yellow 1=red
int currentPlayer=0;
int[] isItDone={2, 2, 2, 2, 2, 2, 2, 2, 2};
//2=yellow 1=red
int[] whoWon=new int[9];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void currentCounter(View view){
ImageView counter= (ImageView) view;
int tap=Integer.parseInt(counter.getTag().toString());
if(isItDone[tap]==2){
counter.setTranslationY(-1000f);
if(currentPlayer==0){
counter.setImageResource(R.drawable.yellow);
currentPlayer=1;
whoWon[tap]=2;
if((whoWon[0]==2&&whoWon[1]==2&&whoWon[2]==2)||(whoWon[3]==2&&whoWon[4]==2&&whoWon[5]==2)||
(whoWon[6]==2&&whoWon[7]==2&&whoWon[8]==2)||(whoWon[0]==2&&whoWon[3]==2&&whoWon[6]==2)||
(whoWon[1]==2&&whoWon[4]==2&&whoWon[7]==2)||(whoWon[2]==2&&whoWon[5]==2&&whoWon[8]==2)||
(whoWon[0]==2&&whoWon[4]==2&&whoWon[8]==2)||(whoWon[2]==2&&whoWon[4]==2&&whoWon[6]==2))
{
Toast.makeText(this,"Yellow Has Won",Toast.LENGTH_LONG).show();
LinearLayout layout = (LinearLayout)findViewById(R.id.linearlayout);
layout.setVisibility(View.VISIBLE);
TextView winnerText= (TextView) findViewById(R.id.winner);
winnerText.setText("Yello Won");
winnerText.setTextColor(Color.WHITE);
}
}
else
{
counter.setImageResource(R.drawable.red);
currentPlayer=0;
whoWon[tap]=1;
if((whoWon[0]==1&&whoWon[1]==1&&whoWon[2]==1)||(whoWon[3]==1&&whoWon[4]==1&&whoWon[5]==1)||
(whoWon[6]==1&&whoWon[7]==1&&whoWon[8]==1)||(whoWon[0]==1&&whoWon[3]==1&&whoWon[6]==1)||
(whoWon[1]==1&&whoWon[4]==1&&whoWon[7]==1)||(whoWon[2]==1&&whoWon[5]==1&&whoWon[8]==1)||
(whoWon[0]==1&&whoWon[4]==1&&whoWon[8]==1)||(whoWon[2]==1&&whoWon[4]==1&&whoWon[6]==1))
{
Toast.makeText(this,"Red Has Won",Toast.LENGTH_LONG).show();
LinearLayout layout = (LinearLayout)findViewById(R.id.linearlayout);
layout.setVisibility(View.VISIBLE);
TextView winnerText= (TextView) findViewById(R.id.winner);
winnerText.setText("Red Won");
winnerText.setTextColor(Color.WHITE);
}
}
counter.animate().translationYBy(1000f).setDuration(300);
isItDone[tap]=3;
}
}
}
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="com.example.achyu.tictactao.MainActivity">
<RelativeLayout
android:layout_width="395dp"
android:layout_height="587dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<LinearLayout
android:id="#+id/linearlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/orange"
android:orientation="vertical"
android:padding="60dp"
android:visibility="invisible">
<TextView
android:id="#+id/winner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="TextView"
android:textSize="40sp"
tools:textColor="#ffffff" />
<Button
android:id="#+id/playagain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<GridLayout
android:layout_width="360dp"
android:layout_height="360dp"
android:background="#drawable/board"
android:columnCount="3"
android:rowCount="3"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
<ImageView
android:id="#+id/imageView0"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:tag="0"
android:layout_row="0"
android:onClick="currentCounter" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_column="1"
android:layout_marginLeft="20dp"
android:tag="1"
android:layout_row="0"
android:onClick="currentCounter" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_column="2"
android:layout_marginLeft="25dp"
android:tag="2"
android:layout_row="0"
android:onClick="currentCounter" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_marginTop="30dp"
android:tag="3"
android:layout_row="1"
android:onClick="currentCounter" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_column="1"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
android:tag="4"
android:layout_row="1"
android:onClick="currentCounter" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_column="2"
android:layout_marginLeft="25dp"
android:layout_marginTop="30dp"
android:tag="5"
android:layout_row="1"
android:onClick="currentCounter" />
<ImageView
android:id="#+id/imageView6"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_marginTop="30dp"
android:tag="6"
android:layout_row="2"
android:onClick="currentCounter" />
<ImageView
android:id="#+id/imageView7"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_column="1"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
android:tag="7"
android:layout_row="2"
android:onClick="currentCounter" />
<ImageView
android:id="#+id/imageView8"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_column="2"
android:layout_marginLeft="25dp"
android:layout_marginTop="30dp"
android:tag="8"
android:layout_row="2"
android:onClick="currentCounter" />
</GridLayout>
</RelativeLayout>
Just put your linear layout below your gridLayout in your relative layout
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.
I have toggle buttons inside linear layout and on click they do not show the ontext and offtext.I also have listener for all toggle buttons and inside onclick function i tried to set the ontext but it didn't work.
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.example.sharmila.eeeeeee.tryfragment"
android:id="#+id/fragment"
android:layout_centerHorizontal="true"
tools:layout="#layout/try_fragment" />
<LinearLayout android:id="#+id/linearlayout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/fragment"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:weightSum="1">
<ToggleButton
android:layout_width="49dp"
android:layout_height="40dp"
android:id="#+id/wholenote"
android:layout_row="0"
android:layout_column="0"
android:background="#drawable/a"
android:textOff=""
android:textOn="S"
android:textSize="700dp"
android:textColor="#ff0000"
/>
<ToggleButton
android:layout_width="49dp"
android:layout_height="40dp"
android:id="#+id/halfnote"
android:layout_row="0"
android:layout_column="1"
android:background="#drawable/b"
android:textOff=""
android:textOn="S"
android:textSize="700dp"
android:textColor="#ff0000"/>
<ToggleButton
android:layout_width="49dp"
android:layout_height="40dp"
android:id="#+id/quarternote"
android:layout_row="0"
android:layout_column="2"
android:background="#drawable/c"
android:textOff=""
android:textOn="S"
android:textSize="700dp"
android:textColor="#ff0000"
/>
<ToggleButton
android:layout_width="49dp"
android:layout_height="40dp"
android:id="#+id/eightnote"
android:layout_row="0"
android:layout_column="3"
android:background="#drawable/d"
android:textOn="S"
android:textSize="700dp"
android:textColor="#ff0000"
android:textOff=""/>
<ToggleButton
android:layout_width="49dp"
android:layout_height="40dp"
android:id="#+id/sixteennote"
android:textOn="S"
android:textSize="700dp"
android:textColor="#ff0000"
android:textOff=""
android:layout_row="0"
android:layout_column="4"
android:background="#drawable/e"
/>
<ToggleButton
android:layout_width="49dp"
android:layout_height="40dp"
android:textOn="S"
android:textSize="700dp"
android:textColor="#ff0000"
android:id="#+id/thirtytwonote"
android:layout_row="0"
android:layout_column="5"
android:background="#drawable/g"
/>
<ToggleButton
android:layout_width="49dp"
android:layout_height="40dp"
android:textOn="S"
android:textSize="700dp"
android:textColor="#ff0000"
android:id="#+id/sixyfournote"
android:layout_row="0"
android:layout_column="6"
android:background="#drawable/f"
/>
</LinearLayout>
<TableRow android:id="#+id/Linearlayout2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/linearlayout"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:weightSum="1">
<View
android:layout_width="10dp"
android:layout_height="40dp"
android:layout_weight="0.34" />
<ImageButton
android:layout_width="49dp"
android:layout_height="40dp"
android:id="#+id/pause"
android:layout_row="0"
android:layout_column="1"
android:src="#drawable/pause" />
<View
android:layout_width="10dp"
android:layout_height="40dp"
android:layout_weight="0.18" />
<ImageButton
android:layout_width="49dp"
android:layout_height="40dp"
android:id="#+id/play"
android:layout_row="0"
android:layout_column="3"
android:src="#drawable/play" />
<View
android:layout_width="10dp"
android:layout_height="40dp"
android:layout_weight="0.18" />
<ImageButton
android:layout_width="49dp"
android:layout_height="40dp"
android:id="#+id/stop"
android:src="#drawable/stop"
android:layout_row="0"
android:layout_column="3"/>
</TableRow>
<GridLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/gridView"
android:layout_below="#+id/Linearlayout2"
>
<Button
android:layout_width="78dp"
android:layout_height="wrap_content"
android:text="Prev"
android:id="#+id/previous"
android:layout_row="0"
android:layout_column="2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:id="#+id/next"
android:layout_row="0"
android:layout_column="19" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
android:id="#+id/save"
android:layout_row="1"
android:layout_column="1" />
<Button
android:layout_width="99dp"
android:layout_height="wrap_content"
android:text="Finish"
android:id="#+id/finish"
android:layout_row="1"
android:layout_column="21" />
</GridLayout>
Activity code:
private static ToggleButton wholenote;
private static ToggleButton halfnote;
private static ToggleButton quarternote;
private static ToggleButton eighthnote;
private static ToggleButton sixteenthnote;
private static ToggleButton thirtytwonote;
private static ToggleButton sixtyfourthnote;
private static String selected = "01";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wholenote = (ToggleButton) findViewById(R.id.wholenote);
wholenote.setOnClickListener(this);
halfnote = (ToggleButton) findViewById(R.id.halfnote);
halfnote.setOnClickListener(this);
quarternote = (ToggleButton) findViewById(R.id.quarternote);
quarternote.setOnClickListener(this);
eighthnote = (ToggleButton) findViewById(R.id.eightnote);
eighthnote.setOnClickListener(this);
sixteenthnote = (ToggleButton) findViewById(R.id.sixteennote);
sixteenthnote.setOnClickListener(this);
thirtytwonote = (ToggleButton) findViewById(R.id.thirtytwonote);
thirtytwonote.setOnClickListener(this);
sixtyfourthnote = (ToggleButton) findViewById(R.id.sixyfournote);
sixtyfourthnote.setOnClickListener(this);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.wholenote:
wholenote.setTextOn("S");
break;
case R.id.halfnote:
halfnote.setTextOn("S");
break;
case R.id.quarternote:
quarternote.setTextOn("S");
break;
case R.id.eightnote:
eightnote.setTextOn("S");
break;
case R.id.sixteennote:
sixteennote.setTextOn("S");
break;
case R.id.thirtytwonote:
thirtytwonote.setTextOn("S");
break;
case R.id.sixyfournote:
sixtyfournote.setTextOn("S");
break;
}
}
android:textSize="700dp"
It's not showing on off text,it is because you text size is too large to show it on screen.
Set all togglebuttons textSize 10dp.
android:textSize="10dp"