Constructing layout builders
So far in this book, all the layouts we have constructed have been static XML definitions. As you would expect, however, it is perfectly possible to construct and inflate UIs dynamically from our source code. Furthermore, Android layouts lend themselves very nicely to the builder pattern, as we saw with our alert dialog, as they are comprised of an ordered collection of smaller objects.
The following example will follow the builder design pattern to inflate a linear layout from a series of predefined layout views. As before, we will build up from interfaces to abstractions and concrete classes. We will create two kinds of layout item, a title or headline view and a content view. We then make several concrete examples of these that can then be constructed by the builder. As there are some features that all views have in common (text and background colors in this case), we will avoid having to duplicate methods by having another interface, with its own concrete...