--- _db_id: 352 content_type: topic prerequisites: hard: - topics/kotlin/data-binding soft: [] ready: true title: Grid Layout --- Ice Cream Sandwich (ICS) sports two new widgets that have been designed to support the richer user interfaces made possible by larger displays: Space and GridLayout. The most commonly used class for layout in Android is LinearLayout, which allows its children to be aligned in the usual ways: along either the horizontal or vertical axes. It’s often possible to take a complicated layout and break it down into a set of nested linear layouts and, provided this nesting doesn’t get too deep, this is still a good choice for many simple layouts. A number of posts and articles (e.g. Android Layout Tricks #1, Flattening The Stack) have highlighted drawbacks of nested layouts; which fall into three basic categories: - Inability to control alignment along both axes simultaneously - Performance problems in hierarchies that are too deep - Unsuitability for design tools that support free-form editing A simple example of the first problem is the following form: ![](milne-1.png) As the font and the text of the “Email address” label change, we want the label to remain aligned with the baseline of the component to its right, and aligned with the right edge of the label below it. It’s not possible to do this with nested LinearLayouts because the label needs to be aligned with other components both horizontally and vertically. These problems aren’t new to Android, or UI toolkits in general, but we’ve used them to drive our work in enriching platform support for flatter hierarchies. ### GridLayout To provide better support for layouts like these we have added a new layout to the Android framework: GridLayout, which can be used to solve the above problems by dividing the container’s real estate into rows and columns: ![](milne-2.png) Now the “Email address” label can belong both to a row that is baseline-aligned, and a column that is right-aligned. GridLayout uses a grid of infinitely-thin lines to separate its drawing area into: rows, columns, and cells. It supports both row and column spanning, which together allow a widget to occupy a rectangular range of cells that are next to each other. We’ll use the words row, column, and cell in the text below as shorthand for row group, column group and cell group respectively, where groups have one or more contiguous elements. #### Similarities with LinearLayout Wherever possible, GridLayout uses the same conventions as LinearLayout for all its XML API — so it should be easy to start using GridLayout if you’ve already used LinearLayout. In fact, the APIs are so similar that changing a tag name from LinearLayout to GridLayout in an XML file that uses LinearLayout will often produce a similar UI without requiring any other changes. When it doesn’t, you’ll still generally end up with a good starting point for a two-dimensional layout. ### Getting Started Two examples in the samples area of the Android 4.0 SDK show typical use of the programmatic and XML APIs respectively: - https://samples/ApiDemos/src/com/example/android/apis/view/GridLayout0.java - https://samples/ApiDemos/res/layout/grid_layout_1.xml [Both examples produce the same UI.] Here’s a slightly simpler version of the above XML layout. ```