Making Toasts

Now to make the buttons fully armed and operational. You are going to have a press of each button trigger a pop-up message called a toast. A toast is a short message that informs the user of something but does not require any input or action. You are going to make toasts that announce whether the user answered correctly or incorrectly (Figure 1.14).

Figure 1.14  A toast providing feedback

A toast providing feedback

First, return to strings.xml and add the string resources that your toasts will display.

Listing 1.11  Adding toast strings (strings.xml)

<resources>
    <string name="app_name">GeoQuiz</string>
    <string name="question_text">Canberra is the capital of Australia.</string>
    <string name="true_button">True</string>
    <string name="false_button">False</string>
    <string name="correct_toast">Correct!</string>
    <string name="incorrect_toast">Incorrect!</string>
</resources>

To create a toast, you call the following method from the Toast class:

    public static Toast makeText(Context context, int resId, int duration)

The Context parameter is typically an instance of Activity (Activity is a subclass of Context). The second parameter is the resource ID of the string that the toast should display. The Context is needed by the Toast class to be able to find and use the string’s resource ID. The third parameter is one of two Toast constants that specify how long the toast should be visible.

After you have created a toast, you call Toast.show() on it to get it on screen.

In QuizActivity, you are going to call makeText(…) in each button’s listener. Instead of typing everything in, try using Android Studio’s code completion feature to add these calls.

Using code completion

Code completion can save you a lot of time, so it is good to become familiar with it early.

Start typing the code addition shown in Listing 1.12. When you get to the period after the Toast class, a pop-up window will appear with a list of suggested methods and constants from the Toast class.

To choose one of the suggestions, use the up and down arrow keys to select it. (If you wanted to ignore code completion, you could just keep typing. It will not complete anything for you if you do not press the Tab key, press the Return key, or click on the pop-up window.)

From the list of suggestions, select makeText(Context context, int resID, int duration). Code completion will add the complete method call for you.

Fill in the parameters for the makeText method until you have added the code shown in Listing 1.12.

Listing 1.12  Making toasts (QuizActivity.java)

mTrueButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(QuizActivity.this,
                       R.string.correct_toast,
                       Toast.LENGTH_SHORT).show();
            // Does nothing yet, but soon!
    }
});
mFalseButton = (Button) findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(QuizActivity.this,
                       R.string.incorrect_toast,
                       Toast.LENGTH_SHORT).show();
            // Does nothing yet, but soon!
    }
});

In makeText(…), you pass the instance of QuizActivity as the Context argument. However, you cannot simply pass the variable this as you might expect. At this point in the code, you are defining the anonymous class where this refers to the View.OnClickListener.

Because you used code completion, you do not have to do anything to import the Toast class. When you accept a code completion suggestion, the necessary classes are imported automatically.

Now, let’s see your new app in action.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
35.175.172.94