VOOZH about

URL: https://www.geeksforgeeks.org/android/how-to-build-a-simple-reflex-game-in-android/

⇱ How to Build a Simple Reflex Game in Android? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Build a Simple Reflex Game in Android?

Last Updated : 23 Jul, 2025

A reflex game is a simple fun game that measures your responding speed. It is quite simple to make and understand. We will be designing a reflex game that will calculate your responding speed. The rules are simple just press the stop button when you see the change in background color, and the time you took in doing so will be your speed. A faster response will show a better quote of praising than a slower one.

Step by Step Implementation

Step 1: Create a New Project

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

Step 2: Working with the activity_main.xml file

The XML codes are used to build the structure of the activity as well as its styling part. It contains a TextView in the center of the activity to show the game instructions. Then it contains two Buttons, start and stop. Below is the code for the activity_main.xml file.

Step 3: Working with the MainActivity.java file

When a player clicks the Start button the onClickListener() function for that button is called. Inside this function, we will generate a random integer number from 1-10. This number will be the seconds after which the background of the display will change. To generate the number we will be using the Random function. After getting the integer number we will create a Handler class and call a Runnable function after a post delay. The main function of the handler is to schedule messages and runnable. Inside the runnable interface, we will set the background of the screen using the setBackgroundResource() function. We will use System.currentTimeMillis() function to get the current time in milliseconds. First, we will get the time of the system when the background of the screen changes, and then we will get the time of the system when the stop button is clicked. The difference between these times will give the reaction time of the player. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.

Output:

Comment
Article Tags:

Explore