Task 6. Adding Views, Constraints, and Buttons

In this task, we will create two more buttons, update the preexisting button, and adjust the views/constraints of them.

What you'll learn:

Step 1: Viewing Constraint Properties

Constraint Properties

Let’s start editing this using the design layout in the editor.

Let’s examine the Constraint Widget.

Ex. Choose textview_first as the selected view and see how it is constrained to its parent, ConstraintLayout.

Step 2: Adding buttons and Constraining their positions

We will create more buttons to learn how to use/connect constraints.

Let’s start by moving the Next button to below TextView.

Clear Constraints of Selection

Let’s examine the Constraint Widget.

Ex. Choose textview_first as the selected view and see how it is constrained to its parent, ConstraintLayout.

Step 2: Adding buttons and Constraining their positions

We will create more buttons to learn how to use/connect constraints.

Let’s start by moving the Next button to below TextView.

Recreate the top constraint of the TextView
Drag the Next button down towards the middle towards the right side

Let’s move on to creating two more buttons.

Palette panel
Place it below the TextView to the left of the Next button

We will worry about lining the three buttons in a bit.

Step 3: Adding Constraints to the Buttons

We will now constrain the tops of the buttons to the bottom of the TextView. We will start quite similarly to how we constrained the top of the TextView to the top of its parent.

You should see that in the Constraint Widget, the constraints for the Next button should look like this:

Constraint Widget
Constraint Widget

Before moving on, relabel the left and middle button so things are a little clearer about which button is which.

You have now learned how to create buttons and edit their constraints.

Step 4: Extracting String Resources

Hover over the text
Extract string resource

Let’s run the app to make sure it is displayed correctly.

Run the app

Congrats! You’ve learned how to create new string resources by extracting them from existing fields (you can also manually create them like you did with color resources). You also learned how to change its id view.

Step 5: Updating the Next Button

It’s finally time to make some changes to the Next button. Its new role will be to generate and display a random number.

Change the id of the Next button

Step 6: Getting your UI ready for the next task

The next task requires us to make the button interactive. Let’s make sure the UI is ready so we don’t have to worry about it.

Make sure the buttons have the right id and position on the screen

Step 7: Fixing errors

If you edited the XML code directly, you might have run into some errors like these:

Fixing errors

This is because the button ids being referenced (i.e. button and button2) were changed but any references to those weren’t. Thus, these constraints are reference non-existent views.

Update the ids to fix them. This is why we want to Refactor or make sure we keep track of what references an id that we want to change.

Another error you might run into is in the FirstFragment.java and this might show up as an error in your terminal.

Fixing errors

To fix it, simply replace “buttonFirst” with “randomButton”.