Open In App

How to Build a Wordle Game Application in Android?

Last Updated : 23 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Wordle is a famous game in which we have to guess a specific word from the given suggestions. We can get to know the length of the word from that we have to guess the word. In this article, we will take a look at How to create a simple Wordle Game Application in which users will be given 6 chances to guess the word inside our application. Below is the output video in which we will get to know what we are going to build in this article. A sample video is given below to get an idea about what we are going to do in this article.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Step 2: Create a new drawable file for the background of the EditText

Navigate to app>drawable>Right click on it>New Drawable Resource file and name it as back_edt and add the below code to it.

XML




<?xml version="1.0" encoding="utf-8"?>
    <!--on below line adding color and radius for edit text-->
    <solid android:color="@color/white" />
    <corners android:radius="6dp" />
</shape>


Step 3: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#141414"
    tools:context=".MainActivity">
 
    <!--on below line creating a text view for heading-->
    <TextView
        android:id="@+id/idTVGreetHeading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp"
        android:gravity="center"
        android:padding="10dp"
        android:text="Wordle!!"
        android:textAlignment="center"
        android:textColor="@color/white"
        android:textSize="32sp"
        android:textStyle="normal" />
 
    <LinearLayout
        android:id="@+id/idLLWordle"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_marginStart="50dp"
        android:layout_marginTop="170dp"
        android:layout_marginEnd="50dp"
        android:orientation="vertical">
 
        <!-- on below line creating a linear layout for row-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:orientation="horizontal"
            android:weightSum="5">
           
            <!-- on below line creating an edit text for word-->
            <EditText
                android:id="@+id/edt_11"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
           
            <!-- on below line creating an edit text for word-->
            <EditText
                android:id="@+id/edt_12"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
           
            <!-- on below line creating an edit text for word-->
            <EditText
                android:id="@+id/edt_13"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
           
            <!-- on below line creating an edit text for word-->
            <EditText
                android:id="@+id/edt_14"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
           
            <!-- on below line creating an edit text for word-->
            <EditText
                android:id="@+id/edt_15"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
           
        </LinearLayout>
       
        <!-- on below line creating a linear layout for row-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginTop="8dp"
            android:orientation="horizontal"
            android:weightSum="5">
           
            <!-- on below line creating an edit text for word-->
            <EditText
                android:id="@+id/edt_21"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
           
            <!-- on below line creating an edit text for word-->
            <EditText
                android:id="@+id/edt_22"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
 
            <!-- on below line creating an edit text for word-->
            <EditText
                android:id="@+id/edt_23"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
 
            <!-- on below line creating an edit text for word-->
            <EditText
                android:id="@+id/edt_24"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
 
            <!-- on below line creating an edit text for word-->
            <EditText
                android:id="@+id/edt_25"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
        </LinearLayout>
       
        <!-- on below line creating a linear layout for row-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginTop="8dp"
            android:orientation="horizontal"
            android:weightSum="5">
 
            <EditText
                android:id="@+id/edt_31"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
 
            <EditText
                android:id="@+id/edt_32"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
 
            <EditText
                android:id="@+id/edt_33"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
 
            <EditText
                android:id="@+id/edt_34"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
 
            <EditText
                android:id="@+id/edt_35"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
        </LinearLayout>
       
        <!-- on below line creating a linear layout for row-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginTop="8dp"
            android:orientation="horizontal"
            android:weightSum="5">
 
            <EditText
                android:id="@+id/edt_41"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
 
            <EditText
                android:id="@+id/edt_42"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
 
            <EditText
                android:id="@+id/edt_43"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
 
            <EditText
                android:id="@+id/edt_44"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
 
            <EditText
                android:id="@+id/edt_45"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
        </LinearLayout>
       
        <!-- on below line creating a linear layout for row-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginTop="8dp"
            android:orientation="horizontal"
            android:weightSum="5">
 
            <EditText
                android:id="@+id/edt_51"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
 
            <EditText
                android:id="@+id/edt_52"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
 
            <EditText
                android:id="@+id/edt_53"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
 
            <EditText
                android:id="@+id/edt_54"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
 
            <EditText
                android:id="@+id/edt_55"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
        </LinearLayout>
       
        <!-- on below line creating a linear layout for row-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginTop="8dp"
            android:orientation="horizontal"
            android:weightSum="5">
 
            <EditText
                android:id="@+id/edt_61"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
 
            <EditText
                android:id="@+id/edt_62"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
 
            <EditText
                android:id="@+id/edt_63"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
 
            <EditText
                android:id="@+id/edt_64"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
 
            <EditText
                android:id="@+id/edt_65"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:background="@drawable/back_edt"
                android:gravity="center"
                android:maxLength="1"
                android:textSize="24sp"
                android:textStyle="bold" />
           
        </LinearLayout>
 
    </LinearLayout>
 
    <!-- on below linecreating a text view for displaying a message-->
    <TextView
        android:id="@+id/idTVCongo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/idLLWordle"
        android:layout_gravity="center"
        android:layout_margin="8dp"
        android:padding="8dp"
        android:text="Congratulation You have guessed the right word!!"
        android:textAlignment="center"
        android:textColor="@color/white"
        android:textSize="22sp"
        android:textStyle="normal"
        android:visibility="gone" />
 
</RelativeLayout>


Step 4: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




package com.gtappdevelopers.spotify
 
import android.graphics.Color
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.gtappdevelopers.spotify.databinding.ActivityMainBinding
 
class MainActivity : AppCompatActivity() {
 
    // on below line creating a
    // variable for activity binding.
    private lateinit var binding: ActivityMainBinding
 
    // on below line creating a variable
    // for word which user has to guess.
    private val WORD = "dubai"
 
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        // on below line calling method to
        // pass focus to next edit text.
        keepPassingFocus()
         
        // on below line adding text change listener
        // for last edit texts of each row
        binding.edt15.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
 
            }
 
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
 
            override fun afterTextChanged(s: Editable?) {
                if (s?.length == 1) {
                    // on below line calling validate row to
                    // check the word entered in that row.
                    validateRow(
                        binding.edt11,
                        binding.edt12,
                        binding.edt13,
                        binding.edt14,
                        binding.edt15
                    )
                }
            }
 
        })
        binding.edt25.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }
 
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
 
            override fun afterTextChanged(s: Editable?) {
                if (s?.length == 1) {
                    // on below line calling validate row to
                    // check the word entered in that row.
                    validateRow(
                        binding.edt21,
                        binding.edt22,
                        binding.edt23,
                        binding.edt24,
                        binding.edt25
                    )
                }
            }
 
        })
        binding.edt35.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
 
            }
 
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
 
            override fun afterTextChanged(s: Editable?) {
                if (s?.length == 1) {
                    // on below line calling validate row to
                    // check the word entered in that row.
                    validateRow(
                        binding.edt31,
                        binding.edt32,
                        binding.edt33,
                        binding.edt34,
                        binding.edt35
                    )
                }
            }
 
        })
        binding.edt45.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
 
            }
 
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
 
            override fun afterTextChanged(s: Editable?) {
                if (s?.length == 1) {
                    // on below line calling validate row to
                    // check the word entered in that row.
                    validateRow(
                        binding.edt41,
                        binding.edt42,
                        binding.edt43,
                        binding.edt44,
                        binding.edt45
                    )
                }
            }
 
        })
        binding.edt55.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
 
            }
 
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
 
            override fun afterTextChanged(s: Editable?) {
                if (s?.length == 1) {
                    // on below line calling validate row to
                    // check the word entered in that row.
                    validateRow(
                        binding.edt51,
                        binding.edt52,
                        binding.edt53,
                        binding.edt54,
                        binding.edt55
                    )
                }
            }
 
        })
        binding.edt65.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
 
            }
 
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
 
            override fun afterTextChanged(s: Editable?) {
                if (s?.length == 1) {
                    // on below line calling validate row to
                    // check the word entered in that row.
                    validateRow(
                        binding.edt61,
                        binding.edt62,
                        binding.edt63,
                        binding.edt64,
                        binding.edt65
                    )
                }
            }
 
        })
    }
 
 
    private fun makeGameInactive() {
        // on below line disabling all
        // edit text to make game inactive.
        binding.edt11.isEnabled = false
        binding.edt12.isEnabled = false
        binding.edt13.isEnabled = false
        binding.edt14.isEnabled = false
        binding.edt15.isEnabled = false
        binding.edt21.isEnabled = false
        binding.edt22.isEnabled = false
        binding.edt23.isEnabled = false
        binding.edt24.isEnabled = false
        binding.edt25.isEnabled = false
        binding.edt31.isEnabled = false
        binding.edt32.isEnabled = false
        binding.edt33.isEnabled = false
        binding.edt34.isEnabled = false
        binding.edt35.isEnabled = false
        binding.edt41.isEnabled = false
        binding.edt42.isEnabled = false
        binding.edt43.isEnabled = false
        binding.edt44.isEnabled = false
        binding.edt45.isEnabled = false
        binding.edt51.isEnabled = false
        binding.edt52.isEnabled = false
        binding.edt53.isEnabled = false
        binding.edt54.isEnabled = false
        binding.edt61.isEnabled = false
        binding.edt62.isEnabled = false
        binding.edt63.isEnabled = false
        binding.edt64.isEnabled = false
        binding.edt65.isEnabled = false
    }
 
    private fun validateRow(
        edt1: EditText,
        edt2: EditText,
        edt3: EditText,
        edt4: EditText,
        edt5: EditText
    ) {
        // on below line creating variables
        // to get text from edit texts.
        val edt1Txt = edt1.text.toString()
        val edt2Txt = edt2.text.toString()
        val edt3Txt = edt3.text.toString()
        val edt4Txt = edt4.text.toString()
        val edt5Txt = edt5.text.toString()
 
        // on below line creating variables
        // to get each char from word.
        val w1 = WORD[0].toString()
        val w2 = WORD[1].toString()
        val w3 = WORD[2].toString()
        val w4 = WORD[3].toString()
        val w5 = WORD[4].toString()
 
        // on below line comparing if text entered in edt is equal to other words.
        if (edt1Txt == w2 || edt1Txt == w3 || edt1Txt == w4 || edt1Txt == w5) {
            // on below line changing background color of that edt.
            edt1.setBackgroundColor(Color.parseColor("#ffff00"))
        }
        // on below line comparing if text entered in edt is equal to other words.
        if (edt2Txt == w1 || edt2Txt == w3 || edt2Txt == w4 || edt2Txt == w5) {
            // on below line changing background color of that edt.
            edt2.setBackgroundColor(Color.parseColor("#ffff00"))
        }
        // on below line comparing if text entered in edt is equal to other words.
        if (edt3Txt == w1 || edt3Txt == w2 || edt3Txt == w4 || edt3Txt == w5) {
            // on below line changing background color of that edt.
            edt3.setBackgroundColor(Color.parseColor("#ffff00"))
        }
        // on below line comparing if text entered in edt is equal to other words.
        if (edt4Txt == w1 || edt4Txt == w2 || edt4Txt == w3 || edt4Txt == w5) {
            // on below line changing background color of that edt.
            edt4.setBackgroundColor(Color.parseColor("#ffff00"))
        }
        // on below line comparing if text entered in edt is equal to other words.
        if (edt5Txt == w1 || edt5Txt == w2 || edt5Txt == w3 || edt5Txt == w4) {
            // on below line changing background color of that edt.
            edt5.setBackgroundColor(Color.parseColor("#ffff00"))
        }
 
        // on below line checking if word is equal to text in edt
        if (edt1Txt == w1) {
            // on below line changing background color of that edt.
            edt1.setBackgroundColor(Color.parseColor("#33cc33"))
        }
        // on below line checking if word is equal to text in edt
        if (edt2Txt == w2) {
            // on below line changing background color of that edt.
            edt2.setBackgroundColor(Color.parseColor("#33cc33"))
        }
        // on below line checking if word is equal to text in edt
        if (edt3Txt == w3) {
            // on below line changing background color of that edt.
            edt3.setBackgroundColor(Color.parseColor("#33cc33"))
        }
        // on below line checking if word is equal to text in edt
        if (edt4Txt == w4) {
            // on below line changing background color of that edt.
            edt4.setBackgroundColor(Color.parseColor("#33cc33"))
        }
        // on below line checking if word is equal to text in edt
        if (edt5Txt == w5) {
            // on below line changing background color of that edt.
            edt5.setBackgroundColor(Color.parseColor("#33cc33"))
        }
 
        // on below line checking if entered by users is present
        // in the word which user has to find.
        if (edt1Txt != w1 && edt1Txt != w2 && edt1Txt != w3 && edt1Txt != w4 && edt1Txt != w5) {
            // on below line changing background color of that edt.
            edt1.setBackgroundColor(Color.parseColor("#ff3333"))
        }
 
        if (edt2Txt != w1 && edt2Txt != w2 && edt2Txt != w3 && edt2Txt != w4 && edt2Txt != w5) {
            // on below line changing background color of that edt.
            edt2.setBackgroundColor(Color.parseColor("#ff3333"))
        }
 
        if (edt3Txt != w1 && edt3Txt != w2 && edt3Txt != w3 && edt3Txt != w4 && edt3Txt != w5) {
            // on below line changing background color of that edt.
            edt3.setBackgroundColor(Color.parseColor("#ff3333"))
        }
 
        if (edt4Txt != w1 && edt4Txt != w2 && edt4Txt != w3 && edt4Txt != w4 && edt4Txt != w5) {
            // on below line changing background color of that edt.
            edt4.setBackgroundColor(Color.parseColor("#ff3333"))
        }
 
        if (edt5Txt != w1 && edt5Txt != w2 && edt5Txt != w3 && edt5Txt != w4 && edt5Txt != w5) {
            // on below line changing background color of that edt.
            edt5.setBackgroundColor(Color.parseColor("#ff3333"))
        }
 
        // below method is called if user is able to guess the word.
        if (edt1Txt == w1 && edt2Txt == w2 && edt3Txt == w3 && edt4Txt == w4 && edt5Txt == w5) {
            // on below line setting text and changing the visibility of text.
            binding.idTVCongo.text = "Congratulations, you have guessed the right word."
            binding.idTVCongo.visibility = View.VISIBLE
            // on below line calling below method to make game inactive.
            makeGameInactive()
            // on below line displaying toast message.
            Toast.makeText(
                applicationContext,
                "Congratulations, you have guessed the right word..",
                Toast.LENGTH_SHORT
            ).show()
            return
        }
 
        // below method is called if user is not able to guess the word.
        if (edt5.id == R.id.edt_65) {
            // on below line setting text if user is not able to guess
            // the word and changing its visibility
            binding.idTVCongo.text = "Sorry you couldn't guess the word."
            binding.idTVCongo.visibility = View.VISIBLE
            // on below line calling
            // inactive to make game inactive.
            makeGameInactive()
            // on below line displaying toast message
            Toast.makeText(
                applicationContext,
                "Sorry you couldn't guess the word.",
                Toast.LENGTH_SHORT
            ).show()
 
        }
    }
 
    private fun keepPassingFocus() {
        // on below line calling method pass focus to next
        // to pass focus to next edt for row 1.
        passFocusToNextEdt(binding.edt11, binding.edt12)
        passFocusToNextEdt(binding.edt12, binding.edt13)
        passFocusToNextEdt(binding.edt13, binding.edt14)
        passFocusToNextEdt(binding.edt14, binding.edt15)
 
        // on below line calling method pass focus to next
        // to pass focus to next edt for row 2.
        passFocusToNextEdt(binding.edt21, binding.edt22)
        passFocusToNextEdt(binding.edt22, binding.edt23)
        passFocusToNextEdt(binding.edt23, binding.edt24)
        passFocusToNextEdt(binding.edt24, binding.edt25)
 
        // on below line calling method pass focus to next
        // to pass focus to next edt for row 3.
        passFocusToNextEdt(binding.edt31, binding.edt32)
        passFocusToNextEdt(binding.edt32, binding.edt33)
        passFocusToNextEdt(binding.edt33, binding.edt34)
        passFocusToNextEdt(binding.edt34, binding.edt35)
 
        // on below line calling method pass focus to next
        // to pass focus to next edt for row 4.
        passFocusToNextEdt(binding.edt41, binding.edt42)
        passFocusToNextEdt(binding.edt42, binding.edt43)
        passFocusToNextEdt(binding.edt43, binding.edt44)
        passFocusToNextEdt(binding.edt44, binding.edt45)
 
        // on below line calling method pass focus to next
        // to pass focus to next edt for row 5.
        passFocusToNextEdt(binding.edt51, binding.edt52)
        passFocusToNextEdt(binding.edt52, binding.edt53)
        passFocusToNextEdt(binding.edt53, binding.edt54)
        passFocusToNextEdt(binding.edt54, binding.edt55)
 
        // on below line calling method pass focus to next
        // to pass focus to next edt for row 6.
        passFocusToNextEdt(binding.edt61, binding.edt62)
        passFocusToNextEdt(binding.edt62, binding.edt63)
        passFocusToNextEdt(binding.edt63, binding.edt64)
        passFocusToNextEdt(binding.edt64, binding.edt65)
    }
 
    private fun passFocusToNextEdt(edt1: EditText, edt2: EditText) {
        // on below line we are passing focus to
        // next edt is previous one is filled.
        edt1.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }
 
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
 
            override fun afterTextChanged(s: Editable?) {
                if (s?.length == 1) {
                    edt2.requestFocus()
                }
            }
 
        })
    }
}


Now run your application to see the output of it. 

Output:



Like Article
Suggest improvement
Next
Share your thoughts in the comments

Similar Reads