Open In App
Related Articles

How to Build a Simple Reflex Game in Android?

Like Article
Save Article
Report issue

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.


<?xml version="1.0" encoding="utf-8"?>
    <!--TextView to display game instruction-->
        android:text="Click on Start first, and wait
                      until the background color changes.
                      As soon as it changes hit Stop"
        android:textSize="25dp" />
        <!--start button-->
            android:text="Start" />
        <!--stop button-->
            android:text="Stop" />


Step 3: Working with the 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 file. Comments are added inside the code to understand the code in more detail.


import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
    public Button button1, button2;
    public RelativeLayout relativeLayout;
    // runnable function
    Runnable runnable = new Runnable() {
        public void run() {
            // set the background on the screen
            // get the system time in milli second
            // when the screen background is set
            final long time = System.currentTimeMillis();
            // function when stop button is clicked
            button2.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    // get the system time in milli second
                    // when the stop button is clicked
                    long time1 = System.currentTimeMillis();
                    // display reflex time in toast message
                    Toast.makeText(getApplicationContext(), "Your reflexes takes " + (time1 - time) + " time to work", Toast.LENGTH_LONG).show();
                    // remove the background again
    protected void onCreate(Bundle savedInstanceState) {
        relativeLayout = findViewById(;
        button1 = findViewById(;
        button2 = findViewById(;
        // function when the start button is clicked
        button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                // generate a random number from 1-10
                Random random = new Random();
                int num = random.nextInt(10);
                // call the runnable function after
                // a post delay of num seconds
                Handler handler = new Handler();
                handler.postDelayed(runnable, num * 1000);



Last Updated : 15 Jan, 2021
Like Article
Save Article
Share your thoughts in the comments
Similar Reads