Maximizing Application Compatibility

 Maximizing Application Compatibility

With almost two dozen manufacturers developing Android devices, we’ve seen an explosion of different models—each with its own market differentiators and unique characteristics. Users now have choices, but these choices come at a cost.This proliferation of devices has led to what some developers call fragmentation and others call compatibility issues.Terminology aside, it has become a challenging task to develop Android applications that support a broad range of devices. Developers must contend with different platform versions, devices with and without optional hardware such as cameras and keyboards, and variations in screen sizes and resolutions (see Figure 25.1).The list of device differentiators is lengthy, and grows with each new device. Although fragmentation makes the Android app developer’s life more complicated, it’s still possible to develop for and support a variety of devices—even all devices—within a single application.When it comes to maximizing compatibility, you’ll always want to use the following strategies: n Whenever possible, choose the development option that is supported by the widest variety of devices. n Whenever a development decision limits the compatibility of your application (for example, using an API that was introduced in a later API Level or introducing a hardware requirement such as camera support), assess the risk and document this Targeting Different Device Configurations and Languages 524  Figure 25.1 Some Android device statistics regarding platform version and screen density (source: http://j.mp/bnW7OV). n Consider screen size and resolution differences when designing application user interfaces. It is often possible to design very flexible layouts that look reasonable in both portrait and landscape modes, as well as different screen resolutions and sizes. However, if you don’t consider this early, you will likely have to make changes (sometimes painful ones) later on to accommodate these differences. n Test on a wide range of devices early in the development process to avoid unpleasant surprises late in the game. Make sure the devices have different hardware and software, including different versions of the Android platform, different screen sizes, and different hardware capabilities

Designing User Interfaces for Compatibility

Before we show you the many ways in which you can provide custom application resources and code to support specific device configurations, it’s important to remember that you can often avoid needing them in the first place.The trick is to design your initial default solution to be flexible enough to cover any variations.When it comes to user interfaces, keep them simple and don’t overcrowd them.Also, take advantage of the many powerful tools at your disposal: n As a rule of thumb, design for medium- to large-size screens and medium resolutions. Over time, devices trend toward larger screens with higher resolutions. n For View and Layout control width and height attributes, use fill_parent (now match_parent) and wrap_content so that controls scale for different screen sizes and orientation changes. n For dimensions, use the flexible units, such as dp and sp, as opposed to fixed unit types, such as px, mm, and in. n Avoid using AbsoluteLayout and other pixel-perfect settings and attributes. n Use flexible layout controls such as RelativeLayout, LinearLayout, TableLayout, and FrameLayout to design a screen that looks great in both portrait and landscape modes and on a variety of different screen sizes and resolutions.Try the working square principle for organizing screen content—we talk more about this in a moment. n Encapsulate screen content in scalable container controls such as ScrollView and ListView. Generally, you should scale and grow screens in only one direction (vertically or horizontally), but not both. n Don’t provide exact positions for screen elements, sizes, and dimensions. Instead, use relative positions, weights, and gravity. Spending time up-front to get these right saves time later. 526 Chapter 25 Targeting Different Device Configurations and Languages n Provide application graphics of reasonable quality and always keep the original (larger) sizes around in case you need different versions for different resolutions at a later time.There is always a tradeoff in terms of graphic quality versus file size. Find the sweet spot where the graphic scales reasonably well for changes in screen characteristics, without bulking up your application or taking too long to display. Whenever possible, use stretchable graphics, such as Nine-Patch, which allow a graphic to change size based upon the area in which it is displayed in. Tip Looking for information about the device screen? Check out the DisplayMetrics utility class, which, when used in conjunction with the window manager, can determine all sorts of information about the display characteristics of the device at runtime: DisplayMetrics currentMetrics = new DisplayMetrics(); WindowManager wm = getWindowManager(); wm.getDefaultDisplay().getMetrics(currentMetrics);

Supporting Specific Screen Types

Although you generally want to try to develop your applications to be screen independent (support all types of screens, small and large, high density and low), you can specify the types of screens your application can support explicitly when necessary using the Android manifest file tag. By default, your application supports all screen types.You might want to consider this configuration option if your application— say a video app or an eBook reader—does not run well at all on small screens. For more information on this Android Manifest tag, see the Android SDK documentation: http:/ /developer.android.com/guide/topics/manifest/supports-screens-element.html. Tip The Android operating system uses a combination of scaling techniques to help smooth differences in screen size, density, and aspect ratio. For more details, see the Android Best Practices article on supporting multiple screens, referenced in the Resources and More Information section at the end of this chapter. Working with Nine-Patch Stretchable Graphics Phone screens come in various dimensions. It can be handy to use stretchable graphics to allow a single graphic that can scale appropriately for different screen sizes and orientations or different lengths of text.This can save you a lot of time in creating graphics for many different screen sizes.Android supports Nine-Patch Stretchable Graphics for this purpose. Nine-Patch Stretchable Graphics are simply PNG graphics that have patches, or areas of the image, defined to scale appropriately, instead of scaling the entire image as one unit. Often the center segment is transparent. Figure 25.2 illustrates how the image (shown as the square) is divided into nine patches. Nine-Patch Stretchable Graphics can be created from PNG files using the draw9patch tool included with the Tools directory of the Android SDK.The interface .

Formation et coursTélécharger le document complet

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *