260 CHAPTER 8: Android UI Design: Using Advanced ViewGroup Layout Containers
I covered several of the other basic ViewGroup subclasses, such as LinearLayout (Chapter 6),
RelativeLayout (Chapter 7), and FrameLayout (Chapter 11) in the other chapters in this book,
because there is simply no way I can effectively cover the primary user interface layout container
classes which I need you to know all about as an Android developer within the span of one single
chapter. By the end of this book, I will have covered most of those UI layout container classes that
you will likely want to use in your Android 4.x and Android 5.x applications development.
In this chapter, we’ll upgrade the RelativeLayout UI layout container design to a GridLayout UI layout
container design, so that you can learn how to use the GridLayout class, and then we will create a
new UI design using the ViewPager and SlidingPaneLayout classes. We will also take a closer look
at the ActionBar class. Later on, we’ll enhance this sliding UI design, which we will create using the
ImageButton UI widget, using graphic design and animated UI elements which we will learn how to
implement during the next two chapters covering graphics design and 2D animation.
Using the Android GridLayout UI Container
Android’s GridLayout class was added to the Android OS in the Ice Cream Sandwich Version 4.0
release. As you saw in the previous chapter, the RelativeLayout class might also be used to create
grid-like UI designs. The GridLayout class methods and attributes (parameters) are optimized for
creating grid-like UI designs, while at the same time using the smallest amount of system memory
possible. The Android GridLayout class has half of the number of parameters ( 12 versus 24) than the
RelativeLayout class has, making it more memory efficient.
Don’t let the small number of layout parameters fool you into thinking that using a GridLayout is a
cakewalk, as GridLayout is more complicated than it might seem on the surface; after all, using grids
is simple, right? This UI class is actually more akin to a “Grid Layout Engine,” if you will, and is
complex in the sense that you will need to understand how it is going to process your parameters in
order for you to design grid-based UI layouts using its dozen attributes (parameters).
Some of the advanced concepts I’ll cover in this section include grid flexibility, grid spanning, grid
spacing, and multiple gravity constant settings. It is interesting to note that this UI layout container
class features more parent (global layout container) parameters, which are used to set GridLayout
engine (processing) attributes, than child UI element parameters, which is fairly unique within
Android UI layout container classes. This is why using the GridLayout class is more complicated,
because you have to understand how the engine will process its settings.
It is important to note that multiple gravity constant settings are also available with some of the
other UI layout container classes which support the concept of layout gravity, and we will cover this
powerful concept here, because using more than one gravity constant at a time is quite useful in
conjunction with all the features of a GridLayout container.
GridLayout Class: Grid-Based UI Layout Designs
The grid layout which is defined using the GridLayout class is comprised of a collection or array of
“infinitely thin” grid lines. These UI grid lines, which are not really there, and which are not actually
(visibly) drawn, separate the grid UI viewing area into something called “cells.”