Reputation: 131
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>
Upvotes: 0
Views: 3236
Reputation: 1519
Just put your linear layout below your gridLayout in your relative layout
Upvotes: 2