Chapter 1. 

Introducing the constraint layout

In this report we’re going to focus on using a new type of layout called a constraint layout. This type of layout is more complex than many others, but it’s a lot more flexible. It’s also much more efficient for complex UIs as you don’t need to nest multiple layouts, so Android has less processing to do at runtime.

To build constraint layouts using the visual tools, you need Android Studio 2.3 or above. If you’re using an older version, check for updates.

You design constraint layouts VISUALLY

Another advantage of using constraint layouts is that they’re specifically designed to work with Android Studio’s design editor. Unlike other layouts where you usually hack direct in XML, you build constraint layouts visually. You drag and drop GUI components onto the design editor’s blueprint tool, and give it instructions for how each view should be displayed.

To see this in action, we’re going to take you on a tour of using a constraint layout, then build the following UI:

image with no caption

Create a new project

We’ll start by creating a new project. Click on the “Start a new Android Studio project” option from the Android Studio welcome screen, then name the project “My Constraint Layout”, and give it a company domain of “hfad.com”, making the package name com.hfad.myconstraintlayout. Select the option to create a phone and tablet app with a minimum SDK of API 19 so that ...

Get Using Constraint Layouts in Android Studio now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.