Open In App
Related Articles

Guessing the Number Game using Android Studio

Improve Article
Save Article
Like Article

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.

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

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.


    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android=""
                android:text="GUESS THE NUMBER"
                android:textSize="30sp" />
                android:text="I am thinking a number between 1 to 100. Can you guess what it  is ?"
                app:layout_constraintStart_toStartOf="parent" />
                android:inputType="numberDecimal" />
                app:layout_constraintStart_toStartOf="parent" />


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

Step3: Working with Java file

  • Open the, 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(;

    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 is:

package com.example.guessthenumber;
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)
    public void clickFunction(View view)
        int userGuessing;
        EditText variable
 = (EditText)findViewById(;
 = Integer.parseInt(
        if (userGuessing < result) {
            makeToast("Think of Higher Number, 
Try Again");
        else if (userGuessing > result) {
            makeToast("Think of Lower Number, 
Try Again");
        else {
+" You Got the Number");
    protected void onCreate(
Bundle savedInstanceState)
        int min = 1;
        int max = 100;
        result = getRandomNumber(min, max);


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

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 09 Jul, 2020
Like Article
Save Article
Similar Reads
Complete Tutorials