The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Resource Sets and Configurations

Devices sometimes change while users are using them, in ways that our application will care about:

In all of these cases, it is likely that we will want to change what resources we use. For example, our layout for a portrait screen may be too tall to use in landscape mode, so we would want to substitute in some other layout.

This chapter will explore how to provide alternative resources for these different scenarios — called “configuration changes” — and will explain what happens to our activities when the user changes the configuration while we are in the foreground.

What’s a Configuration? And How Do They Change?

Different pieces of Android hardware can have different capabilities, such as:

Some of these, in the eyes of the core Android team, might drive the selection of resources, like layouts or drawables. Different screen sizes might drive the choice of layout. Different screen densities might drive the choice of drawable (using a higher-resolution image on a higher-density device). These are considered part of the device’s “configuration”.

Other differences — ones that do not drive the selection of resources — are not part of the device’s configuration but merely are “features” that some devices have and other devices do not. For example, cameras and Bluetooth and WiFi are features.

Some parts of a configuration will only vary based on different devices. A screen will not change density on the fly, for example. But some parts of a configuration can be changed during operation of the device, such as orientation (portrait vs. landscape) or language. When a configuration switches to something else, that is a “configuration change”, and Android provides special support for such events to help developers adjust their applications to match the new configuration.

Configurations and Resource Sets

The preview of this section was lost in the sofa cushions.

Screen Size and Orientation

The preview of this section left for Hollywood to appear in a reality TV show.

Coping with Complexity

The preview of this section is in an invisible, microscopic font.

Choosing The Right Resource

The preview of this section was accidentally identified as an Android 'tasty treat' by the Cookie Monster.

API-Versioned Resources

The preview of this section is being chased by zombies.

Default Change Behavior

The preview of this section was stepped on by Godzilla.

State Saving Scenarios

The preview of this section was accidentally identified as an Android 'tasty treat' by the Cookie Monster.

Your Options for Configuration Changes

The preview of this section was accidentally identified as an Android 'tasty treat' by the Cookie Monster.

Blocking Rotations

The preview of this section will not appear here for a while, due to a time machine mishap.

And Now, a Word From the Android Project View

The preview of this section was eaten by a grue.

Configuration Challenges

The preview of this section did not survive Thanos's finger snap.