Step #2: Adding a RecyclerView

In that toolstrip, click the “Design” button, to switch to the design view. Then, in the “Palette” area, switch to “Common” category:

Android Studio Layout Designer Palette
Android Studio Layout Designer Palette

Drag a RecyclerView out of the “Palette” and drop it in the preview area:

Android Studio Layout Designer, Showing Added RecyclerView
Android Studio Layout Designer, Showing Added RecyclerView

This will take up the top or bottom half of the layout, or possibly the full layout, or possibly just the middle of the layout, depending on where you drop it.

Unfortunately, the Android Studio layout editor has many issues, including making the RecyclerView too big to manipulate. Grab a corner of the RecyclerView and drag it inwards to shrink it a bit. Then, drag the RecyclerView away from the edge a bit, to give you room to maneuver on all four sides:

Android Studio Layout Designer, Showing Resized and Moved RecyclerView
Android Studio Layout Designer, Showing Resized and Moved RecyclerView

Hover your mouse over the left edge of the RecyclerView preview rectangle, find the dot towards the center of the left edge, and drag it to connect with the left edge of the preview area, which will connect it to that side of the ConstraintLayout:

Android Studio Layout Designer, Showing RecyclerView Anchored on the Left
Android Studio Layout Designer, Showing RecyclerView Anchored on the Left

Repeat that process on the right side:

Android Studio Layout Designer, Showing RecyclerView Anchored on Both Sides
Android Studio Layout Designer, Showing RecyclerView Anchored on Both Sides

Repeat that process on the top side:

Android Studio Layout Designer, Showing RecyclerView Anchored on Both Sides and the Top
Android Studio Layout Designer, Showing RecyclerView Anchored on Both Sides and the Top

Repeat that process on the bottom side:

Android Studio Layout Designer, Showing RecyclerView Anchored on All Four Sides
Android Studio Layout Designer, Showing RecyclerView Anchored on All Four Sides

In the “Attributes” pane on the right side of the Layout Designer, change the layout_width and layout_height values each to match_constraint (a.k.a., 0dp) from their current fixed values:

Android Studio Layout Designer, Attributes Pane, Showing New Sizes (Highlighted)
Android Studio Layout Designer, Attributes Pane, Showing New Sizes (Highlighted)

Now, you should see our RecyclerView fill the entire space. More importantly, we taught the RecyclerView to fill the entire space, no matter what the screen size is. Before, the RecyclerView would have some fixed size, regardless of whether the screen is larger or smaller than that size.

Back in the “Attributes” pane, give the RecyclerView an ID of items, via the field at the top:

Android Studio Layout Designer, Attributes Pane, ID Highlighted
Android Studio Layout Designer, Attributes Pane, ID Highlighted

Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.