Guessing the Number Game using Android Studio Last Updated : 09 Jul, 2020 Comments Improve Suggest changes 6 Likes Like 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:app="http://schemas.android.com/apk/res-auto" 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 Create Quiz Comment A arjun_rajput Follow 6 Improve A arjun_rajput Follow 6 Improve Article Tags : Android Android Projects Explore BasicsIntroduction to Android Development 5 min read History of Android 15+ min read Best Way to Become Android Developer â A Complete Roadmap 7 min read Android Development Prerequisites [2025] - Things to Learn Before Android Development 8 min read Android App Development Fundamentals for Beginners 6 min read Android Architecture 5 min read Android System Architecture 3 min read Android Boot Process 4 min read Difference between Java and Kotlin in Android with Examples 3 min read Interesting Facts About Android 3 min read Software Setup and ConfigurationDownload and Install JDK on Windows, Mac and Linux 6 min read Guide to Install and Setup IntelliJ IDEA for Android App Development 5 min read Guide to Install and Setup Visual Studio for Android App Development 4 min read How to Run the Android App on a Real Device? 2 min read Resolving frequently occurring errors in Android Development 3 min read Android Studio Tutorial 9 min read File Structure & ComponentsComponents of an Android Application 3 min read Introduction to Activities in Android 6 min read Services in Android with Example 10 min read Core TopicsHow Does Android App Work? 7 min read Activity Lifecycle in Android with Demo App 9 min read Introduction to Gradle 4 min read What is Context in Android? 9 min read Bundle in Android with Example 6 min read Activity State Changes In Android with Example 6 min read Processes and Application Lifecycle in Android 7 min read Desugaring in Android 4 min read Difference Between AndroidX and Android Support Libraries 3 min read Memory Leaks in Android 7 min read Layout & ViewLayouts in Android UI Design 3 min read Android UI Layouts 5 min read LinearLayout and its Important Attributes with Examples in Android 3 min read Android LinearLayout in Kotlin 2 min read Android RelativeLayout in Kotlin 4 min read ConstraintLayout in Android 6 min read TextView widget in Android with Examples 5 min read TextView in Kotlin 3 min read Working With the TextView in Android 7 min read Autosizing TextView in Android 6 min read ButtonButton in Android 3 min read How to Add Radio Buttons in an Android Application? 5 min read RadioButton in Kotlin 4 min read How to add Toggle Button in an Android Application 3 min read ToggleButton in Kotlin 2 min read RadioGroup in Kotlin 3 min read Intent and Intent FiltersWhat is Intent in Android? 4 min read Implicit and Explicit Intents in Android with Examples 6 min read How to Send Data From One Activity to Second Activity in Android? 7 min read How to open dialer in Android through Intent? 3 min read Creating Multiple Screen Applications in Android 6 min read How to Open Camera Through Intent and Display Captured Image in Android? 6 min read Toast & RecyclerViewToasts for Android Studio 2 min read What is Toast and How to Use it in Android with Examples? 6 min read Android Toast in Kotlin 3 min read How to Change Toast font in Android? 3 min read How to add a custom styled Toast in Android 4 min read RecyclerView in Android with Example 7 min read Android | Horizontal RecyclerView with Examples 4 min read How to create a nested RecyclerView in Android 5 min read How to Create RecyclerView with Multiple ViewType in Android? 6 min read RecyclerView using ListView in Android With Example 5 min read Fragments & AdaptersIntroduction to Fragments | Android 5 min read Fragment Lifecycle in Android 8 min read How to Create a New Fragment in Android Studio? 2 min read How to Create Swipe Navigation in Android? 6 min read ViewPager Using Fragments in Android with Example 6 min read ArrayAdapter in Android with Example 3 min read SimpleAdapter in Android with Example 7 min read SimpleExpandableListAdapter in Android with Example 10 min read AdapterViewFlipper in Android with Example 5 min read BaseExpandableListAdapter in Android with Example 10 min read Like