Chapter 16: Implementing Responsive User Interfaces

The previous chapter introduced the theory behind using responsive design to support multiple screen sizes. This chapter dives into the technical side of the same concept. You’ll learn all about fragments, including how to use them to create responsive user interfaces. After introducing the concept of fragments, the chapter goes through a full example of a responsive layout and also talks about migrating existing apps.

Introduction to Fragments

Fragments are the key to implementing responsive user interfaces on Android. A fragment is an independent user interface section that can be added to a layout. Probably the best way to think about fragments is to think of them as sub-activities. Fragments are always controlled by an activity and their lifecycle is bound to the activity’s lifecycle unless you manually change this aspect.

You implement fragments in a very similar way that you create activities. You create the implementing class and set a layout to draw the fragment’s user interface. Fragments aren’t required to have a user interface, but the fragments discussed in the scope of this book do all have fragments.

Creating fragments

To create a fragment, you extend the Fragment class or one of its subclasses (DialogFragment, ListFragment, PreferenceFragment, or WebViewFragment).

Most of your knowledge of working with activities is applicable to working with fragments, with a few notable exceptions. First, many of the utility ...

Get Smashing Android UI now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.