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.17).

Figure 1.17  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.10  Adding toast strings (res/values/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>

Next, update your click listeners to create and show a toast. Use code completion to help you fill in the listener code. Code completion can save you a lot of time, so it is good to become familiar with it early.

Start typing the code shown in Listing 1.11 in MainActivity.kt. When you get to the period after the Toast class, a pop-up window will appear with a list of suggested functions 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, resId: Int, duration: Int). Code completion will add the complete function call for you.

Fill in the parameters for the makeText(…) function until you have added the code shown in Listing 1.11.

Listing 1.11  Making toasts (MainActivity.kt)

override fun onCreate(savedInstanceState: Bundle?) {
    ...
    trueButton.setOnClickListener { view: View ->
        // Do something in response to the click here
        Toast.makeText(
                this,
                R.string.correct_toast,
                Toast.LENGTH_SHORT)
                .show()
    }

    false.setOnClickListener { view: View ->
        // Do something in response to the click here
        Toast.makeText(
                this,
                R.string.incorrect_toast,
                Toast.LENGTH_SHORT)
                .show()
    }
}

To create a toast, you call the static function Toast.makeText(Context!, Int, Int). This function creates and configures a Toast object. The Context parameter is typically an instance of Activity (and Activity is a subclass of Context). Here you pass the instance of MainActivity as the Context argument.

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 onscreen.

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 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
3.147.104.248