194
CHAPTER 9: User Interface
Finding a way your app can run with different hardware features,
including sensors and keyboards
Finding a way you can filter your app’s visibility inside the Google
Play store
Possibly providing different APKs for one app, depending on device
features
In this chapter, we talk about UI-related issues for compatibility; we focus on screen and
user input capabilities.
Screen Sizes
To allow your app to look nice on different screen sizes, you can do the following:
Use flexible layouts
Avoid specifying absolute positions and absolute width. Instead, use
layouts that allow specifications like “on the right of” or “use half of the
available space” or similar.
Use alternative layouts
Using alternative layouts is a powerful means for supplying different screen
sizes. The layout XML files can be put into different directories with names
containing size filters. For example, you could put one layout into the file
res/layout/main_activity.xml and another one into res/layout-sw480dp/
main_activity.xml expressing a “smallest width” of 480dp (for large phone
screens 5" or higher). The naming schema gets extensively described
online in the “providing resources” and “providing alternative resources”
documents in the Android developer documentation. The Android OS then
automatically picks the best matching layout during runtime on the user’s
device.
Use stretchable images
You can provide nine-patch bitmaps for UI elements. Inside such images
you provide a 1-pixel-wide border telling which parts of an image can
be repeated to stretch an image and optionally which parts can be used
for inner contents. Such nine-patch images are PNG files with the suffix
.9.png. Android Studio allows for converting conventional PNGs into nine-
patch PNGs; use the context menu for that purpose.
Pixel Densities
Devices have different pixel densities. To make your app as device independent as possible,
wherever you need pixel sizes, use density-independent pixel sizes instead of pixel sizes.
Density-independent pixel sizes use dp as a unit, while pixels use px.
In addition, you can also provide different layout files based on different densities. The
separation is similar to the separation for different screen sizes described earlier.
Do'stlaringiz bilan baham: |