How To Create Tic-Tac-Toe In Android Studio in easy steps - Android Hire
Android Phones

How To Create Tic-Tac-Toe In Android Studio in easy steps

Dhaval
First, you need some drawable resources to make the UI as you see below. You can get drawable from this link.
After downloading the drawable paste them inside the drawable folder of your project.
Now define the following colours in your colors.xml file.
Tic-Tac-Toe In Android Studio

In this tutorial, we will develop a super cool Tic-Tac-Toe in android studio that is impossible to beat.

Must be thinking why impossible! Because we are going to use Artificial Intelligence in this game.

Creating Tic-Tac-Toe in Android Studio

  1. Create a new project by going to File ⇒ New Android Project. and fill the required details.
  2. Implement your main Activity class from TicTacToe.OnInitListener
  3. Now add following code your class

Creating Game UI:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3991aa</color>
    <color name="colorPrimaryDark">#184b5a</color>
    <color name="colorBackground">#26697c</color>
    <color name="colorAccent">#0A3664</color>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="3">
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="3">
      <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40sp" 
<Butto
             android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40sp”
  <Button
            android:id="@+id/btn3"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1”
   android:textSize="40sp" 
</LinearLayou>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="3">
  <Button
            android:id="@+id/btn4"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40sp" />
   <Button
            android:id="@+id/btn5"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40sp" />
 <Button
            android:id="@+id/btn6"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40sp" /
   </LinearLayout>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="3">
<Button
            android:id="@+id/btn7"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40sp" />
<Button
            android:id="@+id/btn8"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40sp" />
<Button
            android:id="@+id/btn9"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40sp" 
 </LinearLayout>
<Button
        android:id="@+id/reset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="RESET"
        android:textSize="25sp" />
</LinearLayout>

MainActivity.java:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
int chance=0;
    Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,reset;
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main)
 btn1=(Button)findViewById(R.id.btn1);
 btn2=(Button)findViewById(R.id.btn2);
 btn3=(Button)findViewById(R.id.btn3);
 btn4=(Button)findViewById(R.id.btn4);
 btn5=(Button)findViewById(R.id.btn5);
 btn6=(Button)findViewById(R.id.btn6);
 btn7=(Button)findViewById(R.id.btn7);
 btn8=(Button)findViewById(R.id.btn8);
 btn9=(Button)findViewById(R.id.btn9);
    reset=(Button)findViewById(R.id.reset);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
        btn8.setOnClickListener(this);
        btn9.setOnClickListener(this);
        reset.setOnClickListener(this);
 }
    public void onClick(View view) {
    switch(view.getId())
 case R.id.btn1.
              if(btn1.getText().toString().equals("")){
                    if(chance==0){
                        chance=1;
                        btn1.setText("O");
  result()
                    }
                    else{
                        chance=0;
                        btn1.setText("X");
                        result();
}
}
break;
            case R.id.btn2:
                if(btn2.getText().toString().equals("")){
                    if(chance==0){
                        chance=1;
                        btn2.setText("O");
                        result();
                    }
                    else{
                        chance=0;
                        btn2.setText("X");
                        result();
                    }
                }
                break;
            case R.id.btn3:
                if(btn3.getText().toString().equals("")){
                    if(chance==0){
                        chance=1;
                        btn3.setText("O");
                        result();
                    }
                    else{
                        chance=0;
                        btn3.setText("X");
                        result();
}
}
                break;
            case R.id.btn4:
                if(btn4.getText().toString().equals("")){
                    if(chance==0){
                        chance=1;
                        btn4.setText("O");
                        result();
}
                    else{
                        chance=0;
                        btn4.setText("X");
                        result();
}
}
                break;
            case R.id.btn5:
                if(btn5.getText().toString().equals("")){
                    if(chance==0){
                        chance=1;
                        btn5.setText("O");
                        result();
}
                    else{
                        chance=0;
                        btn5.setText("X");
                        result();
}
}
                break;
            case R.id.btn6:
                if(btn6.getText().toString().equals("")){
                    if(chance==0){
                        chance=1;
                        btn6.setText("O");
                        result();}
                    else{
                        chance=0;
                        btn6.setText("X");
                        result();
}
}
                break;
            case R.id.btn7:
                if(btn7.getText().toString().equals("")){
                    if(chance==0){
                        chance=1;
                        btn7.setText("O");
                        result();
}
                    else{
                        chance=0;
                        btn7.setText("X");
                        result();
}
}
                break;
            case R.id.btn8:
                if(btn8.getText().toString().equals("")){
                    if(chance==0){
                        chance=1;
                        btn8.setText("O");
                        result();
}
                   else{
                        chance=0;
                        btn8.setText("X");
                        result();
}
}
                break;
            case R.id.btn9:
                if(btn9.getText().toString().equals("")){
                    if(chance==0){
                        chance=1;
                        btn9.setText("O");
                        result();
}
                   else{
                        chance=0;
                        btn9.setText("X");
                        result();
}
}
  break;
                case R.id.reset:
                btn1.setText("");
                btn2.setText("");
                btn3.setText("");
                btn4.setText("");
                btn5.setText("");
                btn6.setText("");
                btn7.setText("");
                btn8.setText("");
                btn9.setText("");
}
}
 public void result(){
        if(btn1.getText().toString().equals("X") && btn2.getText().toString().equals("X") && btn3.getText().toString().equals("X")){

            Toast.makeText(this, "Player X wins", Toast.LENGTH_SHORT).show();
        reset.callOnClick();
}
     else if(btn4.getText().toString().equals("X") && btn5.getText().toString().equals("X") && btn6.getText().toString().equals("X")){

            Toast.makeText(this, "Player X wins", Toast.LENGTH_SHORT).show();
            reset.callOnClick();
}
         else if(btn7.getText().toString().equals("X") && btn8.getText().toString().equals("X") && btn9.getText().toString().equals("X")){

            Toast.makeText(this, "Player X wins", Toast.LENGTH_SHORT).show();
            reset.callOnClick();
}
          else if(btn1.getText().toString().equals("O") && btn2.getText().toString().equals("O") && btn3.getText().toString().equals("O")){

            Toast.makeText(this, "Player O wins", Toast.LENGTH_SHORT).show();
            reset.callOnClick();
}
         else if(btn4.getText().toString().equals("O") && btn5.getText().toString().equals("O") && btn6.getText().toString().equals("O")){

            Toast.makeText(this, "Player O wins", Toast.LENGTH_SHORT).show();
            reset.callOnClick();
}
          else if(btn7.getText().toString().equals("O") && btn8.getText().toString().equals("O") && btn9.getText().toString().equals("O")){

            Toast.makeText(this, "Player O wins", Toast.LENGTH_SHORT).show();
            reset.callOnClick();
}
          else  if(btn1.getText().toString().equals("X") && btn4.getText().toString().equals("X") && btn7.getText().toString().equals("X")){

            Toast.makeText(this, "Player X wins", Toast.LENGTH_SHORT).show();
            reset.callOnClick();
}
          else if(btn2.getText().toString().equals("X") && btn5.getText().toString().equals("X") && btn8.getText().toString().equals("X")){

            Toast.makeText(this, "Player X wins", Toast.LENGTH_SHORT).show();
            reset.callOnClick();
}
            else if(btn3.getText().toString().equals("X") && btn6.getText().toString().equals("X") && btn9.getText().toString().equals("X")){

            Toast.makeText(this, "Player X wins", Toast.LENGTH_SHORT).show();
            reset.callOnClick();
}
           else if(btn1.getText().toString().equals("O") && btn4.getText().toString().equals("O") && btn7.getText().toString().equals("O")){

            Toast.makeText(this, "Player O wins", Toast.LENGTH_SHORT).show();
            reset.callOnClick();
}
        else if(btn2.getText().toString().equals("O") && btn5.getText().toString().equals("O") && btn8.getText().toString().equals("O")){

            Toast.makeText(this, "Player O wins", Toast.LENGTH_SHORT).show();
            reset.callOnClick();
}
           else if(btn3.getText().toString().equals("O") && btn6.getText().toString().equals("O") && btn9.getText().toString().equals("O")){

            Toast.makeText(this, "Player O wins", Toast.LENGTH_SHORT).show();
            reset.callOnClick();
}
        else if(btn1.getText().toString().equals("X") && btn5.getText().toString().equals("X") && btn9.getText().toString().equals("X")){

            Toast.makeText(this, "Player X wins", Toast.LENGTH_SHORT).show();
            reset.callOnClick()
}
 else if(btn3.getText().toString().equals("X") && btn5.getText().toString().equals("X") && btn7.getText().toString().equals("X")){

            Toast.makeText(this, "Player X wins", Toast.LENGTH_SHORT).show();
            reset.callOnClick();
}
         else if(btn1.getText().toString().equals("O") && btn5.getText().toString().equals("O") && btn9.getText().toString().equals("O")){

            Toast.makeText(this, "Player O wins", Toast.LENGTH_SHORT).show();
            reset.callOnClick();
   }
          else if(btn3.getText().toString().equals("O") && btn5.getText().toString().equals("O") && btn7.getText().toString().equals("O")){

            Toast.makeText(this, "Player O wins", Toast.LENGTH_SHORT).show();
            reset.callOnClick();
}
}
}

Final Words

If you use the above code and program it on your Android Studio, you would be able to develop and run your own Tic-Tac-Toe Game for android. If you have any queries or suggestions, do let us know in the comments and we would be there to help you out in your queries.

Dhaval

Dhaval is a consummate tech enthusiast with a penchant for software development and game creation. His technical prowess is reflected not only in his innovative projects but also in the insightful articles he pens. Beyond the world of technology, Dhaval immerses himself in the world of literature, indulging in novels that captivate his imagination during his leisure hours.

Related Posts

7 Top Samsung Galaxy Ring Alternatives for 2025

7 Top Samsung Galaxy Ring Alternatives for 2025

Tired of waiting for the Samsung Galaxy Ring to hit the market? You’re not alone. The smart ring market already has many impressive alternatives available now, despite the buzz around Samsung’s upcoming smart ring. These devices pack advanced health tracking and contactless payment features that might surpass Samsung’s planned offerings. The current lineup of smart […]

What Is Quiet Mode on Instagram and How to Activate It

What Is Quiet Mode on Instagram and How to Activate It

Ever wondered what Quiet Mode on Instagram is all about? This simple yet powerful Instagram feature helps you take a break from the constant buzz of notifications and focus on what truly matters. Whether you’re striving for better work-life balance, dedicating time to studying, or simply trying to disconnect from social media distractions, Quiet Mode […]

How to Make a Bed in Minecraft (Step-by-Step Guide)

How to Make a Bed in Minecraft (Step-by-Step Guide)

A bed in Minecraft is very important. It lets you skip the night and set your spawn point, so if you die, you will return to your bed instead of the original world spawn. This guide will show you how to gather materials, craft a bed, and set your spawn point. We’ll also show you how to customize your bed, build bunk […]

10 Best MMORPG Games For Android

10 Best MMORPG Games For Android

Not too long ago, MMORPG games and powerful gaming consoles were mostly exclusive to PCs. They required high-end graphics and systems to deliver their immersive gameplay. But times have changed. With the rise of gaming-oriented smartphones, you can now enjoy PC-like gaming experiences on your Android device. Thanks to these technological advancements and faster internet […]

Roblox: Fruit Battlegrounds codes (January 2025)

Roblox: Fruit Battlegrounds codes (January 2025)

Fruit Battlegrounds codes are all about getting more gems and help you to shoot up your rank in this One Piece anime-inspired game. This Fruit Battlegrounds was made by Popo developer. It is an action-packed game where players battle it out using unique fruit-based abilities. With constant updates, new fruits, and exciting challenges, it’s a fruity frenzy you won’t […]

Roblox: Ultimate Football Codes (January 2025)

Roblox: Ultimate Football Codes (January 2025)

Want to get some extra items for Ultimate Football in Roblox? You’ve come to the right place! Here’s the latest list of codes to help you score touchdowns and look stylish on the field. These codes offer free rewards such as coins and cosmetics to enhance your gameplay. What are Ultimate Football Codes? Ultimate Football […]

Roblox: Da Hood Codes (January 2025)

Roblox: Da Hood Codes (January 2025)

Are you a fan of Roblox games, in this article we will look at the Roblox Da Hood Codes for December 2024 that will help you unlock exclusive items, improve your gameplay and dominate the streets of Da Hood. You can feel that the game is inspired by the Grand Theft Auto series and there […]