Handling Different Screen Orientations

Android applications can run in landscape or portrait mode, depending on how the user tilts the handset screen. Besides internationalization, one of the most common situations in which applications might want to customize resources is to handle screen orientation changes.

Adding a Custom Layout for Landscape Mode

Thus far, you have been developing and testing the Been There, Done That! application in portrait mode. Launch it now and change to landscape mode before launching the application. Review each screen. You'll note that some screens display fine (they have flexible layouts that work well in either landscape or portrait mode) and some screens, such as the game screen (see Figure 20.1), could use some work.

FIGURE 20.1

The game screen in landscape mode (default layout used).

Modifying how a screen displays based on the orientation of the device is as simple as adding a new set of layout resource files. To do this, you need to do the following:

► Use all currently defined layout resources as the defaults.

► Design a new version of each layout that also looks nice in landscape mode.

► Add these new landscape-specific layouts to the / res/layout-land/ directory.

Let's give this a shot by providing two different versions of the game screen in the Been There, Done That! application—one for portrait mode (the default) and one for landscape mode.

The full layout implementation for the landscape mode changes and handset differences discussed in this hour is available on the book's website, http://www.informit.com/title/9780321673350.

First, let's review the design of the existing game.xml layout file, shown in Figure 20.2.

FIGURE 20.2

The game screen design (default layout version).

In landscape mode, you just don't have enough vertical space for the ImageView, TextView, and Button controls. To streamline this layout for landscape mode, you might consider putting the TextView control to the right of the ImageView control. You could also modify the size of the graphic or the buttons if you needed to. The same controls (with the same names) are defined in both versions of the layout file; they're just rearranged in the landscape version (see Figure 20.3).

FIGURE 20.3

The game screen design (custom landscape layout version).

Icon

Icon

Graphic

TRIVIA QUESTION HERE?

Icon

Icon

TRIVIA QUESTION HERE?

The resulting screen looks much nicer when you switch to landscape mode, as shown in Figure 20.4.

FIGURE 20.4

The game screen in landscape mode (landscape-specific layout used).

WOSt!

Because screen orientation changes cause the current activity to be restarted, any processing tasks such as image decoding or network operations begin again unless you implement the onRetainNonConfigurationInstance() method of the Activity class. For more information on this type of situation, see the write-up at the Android developer website: http://developer.android.com/resources/articles/ faster-screen-orientation-change.html.

Listening for Screen Orientation Changes

Applications can register to listen for screen orientation events. To do this, you request SensorManager by using the getSystemService() method. You can then query SensorManager for the current orientation, using the getOrientation() method. Alternatively, you can implement the OrientationEventListener class (android.view.OrientationEventListener) and override onOrientation Changed() to register for orientation changes.

However, listening for orientation changes is necessary only when applications require special internal handling of orientation. An application that defines a landscape layout resource in the / res/layout-land/ directory and a default portrait layout resource in the / res/layout / directory will work seamlessly, without the need for a listener.

Did you Know?

You can toggle the orientation of the emulator by pressing Ctrl+F11 and Ctrl+F12.

Strategies for Handling Screen Orientation

The best way to support different orientations is to design simple enough layouts that work in either portrait or landscape mode, without modifications. For example, the settings screen of the Been There, Done That! application works fine in both landscape and portrait modes because each setting is stacked in a LinearLayout control, within a scrolling area that can scale well to any size. However, some layouts, such as the splash or game screen, may need some special tweaking for each orientation.

There are many strategies for supporting different screen sizes and orientations. Here are some tips for developing layouts that work for multiple types of screens:

► Don't crowd screens. Keep them simple.

► Use scalable container views such as ScrollView and ListView.

► Scale and grow screens in only one direction (vertically or horizontally), not both.

► Don't hard code the positions of screen elements. Instead, use relative positions and layouts, such as RelativeLayout.

► Avoid AbsoluteLayout and other pixel-specific layout settings.

► Use stretchable graphics, such as Nine-Patch.

► Keep resources as small as possible, so they load fast when the screen orientations change.

The Android developer website contains a helpful set of guidelines for supporting multiple screens: http://developer.android.com/guide/practices/screens_ support.html.

Did you Know?

0 0

Post a comment