Chapter 4. Constraint Layouts: Draw Up a Blueprint
You don’t build a house without a blueprint.
And some layouts use blueprints to make sure they look exactly the way you want. In this chapter, we’ll introduce you to Android’s constraint layout: a flexible way of designing more complex UIs. You’ll discover how constraints and bias let you position and size your views, irrespective of screen size and orientation. You’ll find out how to keep views in their place with guidelines and barriers. Finally, you’ll learn how to pack or spread views with chains and flows. Let’s get designing…
Nested layouts revisited
In the previous chapter, you learned that you can nest layouts to build more complex screens. The following code, for example, uses nested linear layouts to display a text view and edit text in a horizontal row, with an edit text below them:
Nesting layouts comes at a price
The downside to nested layouts is that building complex layouts in this way can be inefficient, make your code harder to read and maintain, and can also slow down your app.
When Android displays a layout on the device screen, it first checks the structure of the layout file, and uses this to build a hierarchy of views. For the nested layout shown on the previous page, for example, it builds a hierarchy ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access