• 20 hours
  • Easy

Free online content available in this course.



Got it!

Last updated on 6/21/19

Improve the User Experience

Log in or subscribe for free to enjoy all this course has to offer!

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 recap!

Our application is much more effective and user-friendly now. The player can understand the sequence of the game without confusion. 🤘

Before moving on, make sure you can remember the following:

  • What steps do you need to complete to delay interface updates?

  • Why is it useful to do so? 

  • How do you make sure a user doesn't click more than one response (or the same response multiple times) before the next question appears? What steps should you follow? 

In the next chapter, we'll learn a little more about how our application works by looking at the Activity Lifecycle.

Example of certificate of achievement
Example of certificate of achievement