Task 7: Making your app interactive

Currently you have buttons on your screen but they don’t do anything when pressed. In this task, you will make your buttons respond.

First, you will make the Toast button show a pop-up message called a toast. After that, you will make the Count button update the number displayed in TextView.

What you will learn

Step 1: Enabling Auto Imports

To make your life easier, you can enable auto-imports so that Android Studio automatically imports any classes that are needed by the Java code.

Step 2: Showing a Toast

In this step, you will attach a Java method to the Toast button to show a toast when the user presses the button. A toast is a short message that appears briefly at the bottom of the screen.

You have learned that to make a view interactive you need to set up a click listener for the view which says what to do when the view (button) is clicked on. The click listener can either:

Step 2: Make the Count button update the number on the screen

Update the Count button so that when it is pressed, the number on the screen increases by 1.

If showCountTextView is showing an error, make sure that it is created just below the FirstFragment class declaration.

showCountTextView

Step 3: Caching the TextView for repeated use

You could call findViewById() in countMe() to find showCountTextView. However, countMe() is called every time the button is clicked, and findViewById() is a relatively time consuming method to call. So it is better to find the view once and cache it.