top of page

Task 7: Make Interactive

  • You will make the Toast button show a pop-up message called a toast. Next, you will make the Count button update the number that is displayed in the TextView.

  • Step 1: Enable 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.

    • In Android Studio, open the settings editor by going to File > Other Settings > Preferences for New Projects.
      Select Auto Imports. In the Java section, make sure Add Unambiguous Imports on the fly is checked.

      • ​​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

 

  • ​Close the settings editor by pressing OK.

  • Step 2: Show 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.

​

​

​

​

​

​

​

​

​

​

​

​

​​

  • Open FirstFragment.java (app > java > com.example.android.myfirstapp > FirstFragment).

    • This class has only two methods, onCreateView() and onViewCreated(). These methods execute when the fragment starts.

    • As mentioned earlier, the id for a view helps you identify that view distinctly from other views. Using the findViewByID() method, your code can find the random_button using its id, R.id.random_button. 2. Take a look at onViewCreated(). It sets up a click listener for the random_button, which was originally created as the Next button.

​

​

​

​

​

​

​​

​

  • ​​​
    • ​Here is what this code does:

    • Use the findViewById() method with the id of the desired view as an argument, then set a click listener on that view.
      In the body of the click listener, use an action, which in this case is for navigating to another fragment, and navigate there. (You will learn about that later.)
      Just below that click listener, add code to set up a click listener for the toast_button, which creates and displays a toast. Here is the code:

​

​

​

​

​

​

​

​

  • ​
    • Run the app and press the Toast button. Do you see the toasty message at the bottom of the screen?

​

​

​

​

​

​

​

​

​

​

​

​

  • ​
    • If you want, extract the message string into a resource as you did for the button labels.

​

​

​

​

​

​

​

 

  • ​
  • Step 3: 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.
      In the fragment_first.xml layout file, notice the id for the TextView:

​

​

​​

  • ​​​
    • In FirstFragment.java, add a click listener for the count_button below the other click listeners in onViewCreated(). Because it has a little more work to do, have it call a new method, countMe().

​

​

​

​

  • ​
    • In the FirstFragment class, add the method countMe() that takes a single View argument. This method will be invoked when the Count button is clicked and the click listener called

​

​

​

​

​

​

​

  • ​
    • Get the value of the showCountTextView. You will define that in the next step.

​

​

​

​

​

  • ​
    • Convert the value to a number, and increment it.

​

​

​

​

​

  • ​​

    • Display the new value in the TextView by programmatically setting the text property of the TextView.

​

​

​

​

​​

  • ​​​​

    • Here is the whole method:

​

​

​

​

​

​

​

​

​

​

  • ​​
  • Step 4: Cache 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.

    • In the FirstFragment class before any methods, add a member variable for showCountTextView of type TextView.

​

​

​

​

​

​

  •  
  • ​​

    • In onCreateView(), you will call findViewById() to get the TextView that shows the count. The findViewById() method must be called on a View where the search for the requested ID should start, so assign the layout view that is currently returned to a new variable, fragmentFirstLayout, instead.

​

​

​

​

​

​

  •  
  • ​

    • Call findViewById() on fragmentFirstLayout, and specify the id of the view to find, textview_first. Cache that value in showCountTextView.

​

​

​

​

​

​

    • Return fragmentFirstLayout from onCreateView().

​

​

​

​

​

​

​

  • ​
    • Here is the whole method and the declaration of showCountTextView:

​

​

 

​

​

​

​

​

​

​

​

​

​

​

​

  • ​
    • Run your app. Press the Count button and watch the count update.

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

  • ​​
Screenshot 2024-02-06 at 6.10.08 PM.png
Screenshot 2024-02-06 at 6.10.48 PM.png
Screenshot 2024-02-06 at 6.11.11 PM.png
Screenshot 2024-02-06 at 6.11.44 PM.png
Screenshot 2024-02-06 at 6.10.48 PM.png
Screenshot 2024-02-06 at 6.11.44 PM.png
Screenshot 2024-02-06 at 6.12.37 PM.png
Screenshot 2024-02-06 at 6.12.56 PM.png
Screenshot 2024-02-06 at 6.13.17 PM.png
Screenshot 2024-02-06 at 6.13.41 PM.png
Screenshot 2024-02-06 at 9.41.18 PM.png
Screenshot 2024-02-06 at 9.41.50 PM.png
Screenshot 2024-02-06 at 9.42.27 PM.png
Screenshot 2024-02-06 at 9.42.55 PM.png
Screenshot 2024-02-06 at 9.44.28 PM.png
Screenshot 2024-02-06 at 9.43.46 PM.png
Screenshot 2024-02-06 at 9.45.11 PM.png
Screenshot 2024-02-06 at 9.45.29 PM.png
Screenshot 2024-02-06 at 9.45.50 PM.png

© 2035 by Ingredients. Powered and secured by Wix

  • Black Facebook Icon
  • Black Twitter Icon
  • Black Instagram Icon
bottom of page