Task 8: Implement the second fragment

Congrats on reaching this point of the tutorial! Now we will implement the Random button and its functionality, so that it displays a number between 0 and the current count on a second screen:

The second fragment

The screen for the new fragment will have a heading and the aforementioned random number. In the design view your screen should look like this:

The second fragment

The %d is basically a placeholder for a number (d for decimal). The R is another placeholder.

Step 1: Add a TextView for the random number

This TextView is constrained on all edges, so it's better to use a vertical bias than margins to adjust the vertical position, to help the layout look good on different screen sizes and orientations. 10. If you get a warning "Not Horizontally Constrained," add a constraint from the start of the button to the left side of the screen and the end of the button to the right side of the screen.

Here is the XML code for the TextView that displays the random number:

XML code for the TextView

Step 2: Update the TextView to display the header

Here is the XML code for the TextView that displays the heading:

XML code for the TextView

Step 3: Change the background color of the layout

Give your new activity a different background color than the first activity:

In the Attributes panel:

Attributes panel

Good job! Our app now has a completed layout for the second fragment. But if you run your app and press the Random button, it may crash or not work. The click handler that Android Studio set up for that button needs some changes. In the next task, you will explore and fix this error.

Step 4: Examine the navigation graph

When you created your project, you chose Basic Activity as the template for the new project. When Android Studio uses the Basic Activity template for a new project, it sets up two fragments, and a navigation graph to connect the two. It also set up a button to send a string argument from the first fragment to the second. This is the button you changed into the Random button. And now you want to send a number instead of a string.

A screen similar to the Layout Editor in Design view appears. It shows the two fragments with some arrows between them. You can zoom with + and - buttons in the lower right, as you did with the Layout Editor.

The navigation graph

Step 5: Enable SafeArgs

SafeArgs is required to make the project work! To enable it we do the following:

Step 6: Create the argument for the navigation action

Step 7: Send the count to the second fragment

The Next/Random button was set up by Android Studio to go from the first fragment to the second, but it doesn't send any information. In this step you'll change it to send a number for the current count. You will get the current count from the text view that displays it, and pass that to the second fragment.

Step 8: Update SecondFragment to compute and display a random number

You have written the code to send the current count to the second fragment. The next step is to add code to SecondFragment.java to retrieve and use the current count.

Congratulations, you have built your first Android app!