Chapter 6. Constraint Layouts: Put Things in Their Place

Let’s face it, you need to know how to create great layouts.

If you’re building apps you want people to use, you need to make sure they look exactly the way you want. So far you’ve seen how to use linear and frame layouts, but what if your design is more complex? To deal with this, we’ll introduce you to Android’s new constraint layout, a type of layout you build visually using a blueprint. We’ll show you how constraints let you position and size your views, irrespective of screen size and orientation. Finally, you’ll find out how to save time by making Android Studio infer and add constraints on your behalf.

Nested layouts can be inefficient

You’ve seen how to build a simple user interface using linear layouts and frame layouts, but what if you need to create something more complex? While complex UIs are possible if you nest your linear and frame layouts deep enough, they can slow down your app and make your code hard to read and maintain.

As an example, suppose you wanted to create a layout containing two rows, each containing two items. One possibility would be to create this layout using three linear layouts: one linear layout at the root, and one nested linear layout for each row:

When Android displays a layout on the device screen, ...

Get Head First Android Development, 2nd Edition 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.