Open In App

Guessing the Number Game using Android Studio

Improve
Improve
Like Article
Like
Save
Share
Report

A Simple Guess the number application in which application generates the random number between 1 to 100 and the user has to guess that Number. It is a simple and basic application that uses basic widgets and libraries.
Approach:

Step1: Creating a new project

  • Click on File option at topmost corner in left.
  • Then click on new and open a new project and name it (Here it is named Guess the number).
  • Now select the Empty Activity with language as Java. Don’t change any other option.

  • Note: By default, there will be two files activity_main.xml and MainActivity.java.

Step2: Designing the UI

  • Add the below code in activity_main.xml file. Here two TextViews , one EditText and a Button is added. TextViews are used to display the message and edittext widget is used by the user to enter the guessed number and the button is required to submit the entered value.

    activity_main.xml




    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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"
        tools:context=".MainActivity">
      
        <RelativeLayout
            android:layout_width="409dp"
            android:layout_height="729dp"
            android:background="#F3A68E"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
      
            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_marginStart="77dp"
                android:layout_marginLeft="77dp"
                android:layout_marginTop="47dp"
                android:background="#FF9100"
                android:text="GUESS THE NUMBER"
                android:textSize="30sp" />
      
            <TextView
                android:id="@+id/textView2"
                android:layout_width="391dp"
                android:layout_height="68dp"
                android:layout_alignParentStart="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_marginStart="21dp"
                android:layout_marginLeft="21dp"
                android:layout_marginTop="147dp"
                android:text="I am thinking a number between 1 to 100. Can you guess what it  is ?"
                android:textSize="24sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent" />
      
            <EditText
                android:id="@+id/editId"
                android:layout_width="348dp"
                android:layout_height="67dp"
                android:layout_alignParentTop="true"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_marginTop="271dp"
                android:layout_marginEnd="30dp"
                android:layout_marginRight="30dp"
                android:ems="10"
                android:gravity="center"
                android:hint="ENTER"
                android:inputType="numberDecimal" />
      
            <Button
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentBottom="true"
                android:layout_marginEnd="152dp"
                android:layout_marginRight="152dp"
                android:layout_marginBottom="266dp"
                android:onClick="clickFunction"
                android:text="GUESS"
                android:textSize="30sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent" />
      
        </RelativeLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>

    
    

  • After adding the above code in activity_main.Xml file, the UI will be like:

Step3: Working with Java file

  • Open the MainActivity.java, within the class, add a method getRandomNumber() which will return the random number between 1 to 100.
  • OnCreate() method is invoked when the app is launched therefore getRandomNumber() function is called from inside so that random number is generated.
  • The value from EditText is taken by using below code:

    EditText variable = (EditText)findViewById(R.id.editId);

    here editId is id for EditText and variable is a Variable name.

  • In variable, data stored is in the form of String so it is converted into Integer by using code below:

    userGuessing = Integer.parseInt(variable.getText().toString());

  • OnClick function is invoked when the user clicks the button. Here the value entered by the user is taken and if-else conditions are used to find whether the user guesses the right number or not and if the guessed number is wrong, ask the user to try again.
  • In the end, show Toast which will give the hint to the user to guess the correct number.

Java code for MainActivity.java is:

MainActivity.java




package com.example.guessthenumber;
  
import androidx.appcompat.app.AppCompatActivity;
  
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
  
public class MainActivity extends AppCompatActivity {
  
    int result;
    static int getRandomNumber(int max, int min)
    {
        return (int)((Math.random()
 * (max - min)) + min);
    }
  
    public void makeToast(String str)
    {
        Toast.makeText(
MainActivity.this
str,
 Toast.LENGTH_SHORT)
.show();
    }
    public void clickFunction(View view)
    {
        int userGuessing;
        EditText variable
 = (EditText)findViewById(
R.id.editId);
        userGuessing
 = Integer.parseInt(
variable
.getText()
.toString());
        if (userGuessing < result) {
  
            makeToast("Think of Higher Number, 
Try Again");
        }
        else if (userGuessing > result) {
            makeToast("Think of Lower Number, 
Try Again");
        }
        else {
            makeToast(
"Congratulations,"
+" You Got the Number");
        }
    }
  
    @Override
    protected void onCreate(
Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        int min = 1;
        int max = 100;
        result = getRandomNumber(min, max);
    }
}


Output:

  • If Number guessed is less than Random Number
  • If Number guessed is more than Random Number
  • If Number guessed is equal to Random Number


Last Updated : 09 Jul, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads