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
<?xmlversion="1.0"encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><RelativeLayoutandroid: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"><TextViewandroid: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"/><TextViewandroid: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"/><EditTextandroid: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"/><Buttonandroid: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>chevron_rightfilter_none
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
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Number Guessing Game in Python using Binary Search
- How to install Android Applications in Mobile Phone without USB Cables using Android Studio
- How to implement Swipe Down to Refresh in Android using Android Studio
- Android | AdMob Banner Ads for Android Studio
- Android | AdMob Interstitial Ads for Android Studio
- Android | How to Create/Start a New Project in Android Studio?
- How to build a simple Calculator app using Android Studio?
- How to create a Stopwatch App using Android Studio
- How to build a simple music player app using Android Studio
- How to populate RecyclerView with Firebase data using FirebaseUI in Android Studio
- Toasts for Android Studio
- Guide to Install and Set up Android Studio
- 10 Important Android Studio Shortcuts You Need the Most
- How to scale different Views to all screen sizes in Android Studio?
- How to Upload Project on GitHub from Android Studio?
- Logcat window in Android Studio
- How to Generate Signed Apk in Android Studio?
- Android Studio Setup for Flutter Development
- How to Create Your Own Shortcut in Android Studio?
- How to Convert SVG, PSD Images to Vector Drawable File in Android Studio?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

