• 20 hours
  • Easy

Free online content available in this course.



Got it!

Last updated on 12/21/23

Improve the User Experience

In this short chapter, we will improve the user experience by ensuring that the player is properly informed so that they don't get lost and are happy to use the application.

Improving the User Experience

With the implementation we have in place, you should note the following: as soon as the player selects an answer, we display the next question before the Toast disappears.  

Ideally, we should wait until the Toast disappears before showing the next question, and in the meantime, we should prevent the user from pressing anything.

Delayed Execution

Using a waiting method, we will ask the system to wait two seconds before executing our piece of code. This will allow us to execute code only after the Toast message disappears. Here's the code:

new Handler().postDelayed(new Runnable() {
    public void run() {
        // If this is the last question, ends the game.
        // Else, display the next question.
}, 2000); // LENGTH_SHORT is usually 2 second long

Let's look through this a little more closely:

  • The Handler class lets us ask the system to perform an action.

  • The method to use is called postDelayed().

  • The first parameter is an object of type Runnable, that is, the action you want to perform. The action is determined by the run() method.

  • The second parameter is the delay, in milliseconds. You can adapt the waiting time according to the duration of the Toast you have chosen (LENGTH_SHORT = 2000ms, and LENGTH_LONG = 3500ms).

Now, let's take a minute to really process this. Can you develop an idea of what belongs in the run() method? 

Disable Clicks

As soon as the user has selected a reply and the Toast message is displayed, we must prevent the user from clicking a response until the next question appears.

What issues could arise if we accepted clicks during this time? 🤔

We will use the dispatchTouchEvent() method to block incoming touches. This is an Activity class method which is called whenever a user touches the screen. It is a great place to temporarily ignore touches.

Override this method using the shortcut CTRL + O (O for Override), type the first few letters to find the method, and then press the Enter key. The signature of the method looks like this:

public boolean dispatchTouchEvent(MotionEvent ev);

The boolean we return from this method tells the system whether the user action should be taken into account. If we want to ignore the action, we return false. Otherwise, call the parent class method. For example, the implementation could be:

public boolean dispatchTouchEvent(MotionEvent ev) {    
    return mEnableTouchEvents && super.dispatchTouchEvent(ev);

All we need to do is manage a class attribute (named mEnableTouchEvents), and set it to true when user actions can be taken into account, or false if the system should ignore them.

We leave it to you to figure out where to initialize this variable and when and where to modify it.

Let's Review With a Demo

You can see these steps in the video below:

Let's Recap!

  • The Handler class allows us to communicate with the system to ask it to perform an action.

  • To disable clicks, override the dispatchTouchEvent() method.

Our app is now more effectivethe sequence of the game will be easier for the player to understand, and there will be no surprises in between questions. We’re almost there! 😃 In the next chapter, we’ll take a look at the Activity life cycle.

Ever considered an OpenClassrooms diploma?
  • Up to 100% of your training program funded
  • Flexible start date
  • Career-focused projects
  • Individual mentoring
Find the training program and funding option that suits you best
Example of certificate of achievement
Example of certificate of achievement