Android-compatible applications on different devices

It's no secret that the number of devices on the Android large, they differ in hardware, screen size and quality, processing power, and others. Unlike the iPhone-programmers who know for sure on which device will be running their app, Android-developers need to focus on application compatibility with different devices. 

This article focuses on the interoperability of applications, especially applications display on screens with different resolutions and diagonals.

image

First we need to understand the opportunities offered by Android for the screen.
Basics

Screen size (screen size) - the physical size of the screen, the predefined values: small, normal, large, extra large *.

The geometric factor (aspect ratio) - the ratio of the physical proportions of the screen (width to height), pre-defined values: long (for screens whose size exceeds the width or height of a standard screen size), notlong (for screens whose dimensions correspond to the standard).

Density (density) - the distribution of pixels with respect to the physical size of the screen. The density distribution of pixels is important because the same UI element, expressed in pixels for screens with a lower density will appear more than screens high. Predefined values ​​for the density: ldpi (low), mdpi (medium), hdpi (high), and xhdpi *.

Independent (from density) pixel (density-independent or dp) - "virtual" pixel, which can be used to draw the application UI-elements. This pixel is equivalent to the physical pixel on the screen with a density of 160 dpi.During the execution of OS Android draws element in accordance with the formula pixel = dp * (density/160), where the density - density of the screen.

It is also worth noting that OS Android works with the screen resolution, through the values ​​of the density screen (no tools to work directly with the resolution of the developer does not have).

The figure below shows the correlation between density and size of the screen device with predetermined values ​​of these quantities.

image

* Another point worthy of note: the density xdpi was added to the version of Android 2.2 (API level 8), meaning the screen xlarge - a version of Android 2.3 (API level 9)

Working with the manifest and resource allocation

Starting with version Android 1.6, was added to the manifest tag <support-screens>, which is used to define a class of devices, which may be running. Tag attributes smallScreens, normalScreens, largeScreens, xlargeScreens correspond to certain values ​​above the screen and can be true or false. Default attribute values ​​vary depending on the version of Android (more details can be found here .). In determining the value of an attribute as true, OS Android gets a signal that is compatible with the appropriate type of screen and does not use additional funding for interoperability (which occurs at a value of false). It is also worth noting that these tools (functions) are only for compatibility with larger screen sizes (so, if the value normalScreen - true, the rest - false, the application is also compatible with screens large and xlarge, but not compatible with small) . This tag is also used to filter Market'om Android applications.

The density also has an attribute - anyDensity, which also takes values ​​true / false. If the attribute value - true, OS Android does not use functions for compatibility with different densities of the screen. In this case, the application must use dp for rendering UI elements, or to manage the calculation of the sizes for different densities. If the value - false, OS Android includes functions for scaling the elements in accordance with the density of the screen.

Allocation of resources

OS Android also provides a means to identify resources that will be used for specific screen sizes and densities.Resources are placed in appropriate folders.

res/layout/my_layout.xml // layout for normal screen size
res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-large-land/my_layout.xml // layout for large screen size in landscape mode
res/layout-xlarge/my_layout.xml // layout for extra large screen size

res/drawable-ldpi/my_icon.png // image for low density
res/drawable-mdpi/my_icon.png // image for medium density
res/drawable-hdpi/my_icon.png // image for high density

res/drawable-nodpi/composite.xml // density independent resource 

Interoperability support screens

Briefly support mechanism can be described using the following steps:
  1. OS Android gets a tag attribute values ​​support-screens of the manifesto.
  2. Loaded resources for the appropriate screen size and density (this happens regardless of the data obtained in step 1).
  3. In accordance with Clause 1 OS Android includes / does not include features to ensure compatibility.
  4. Produced drawing elements.

General recommendations for creating compliant application

  • Use the values ​​wrap_content, fill_parent, dp in the mock.
  • Avoid the use of AbsoluteLayout.
  • Use class methods ViewConfiguration for standard values ​​size, speed, time.
  • Use different resources for different values ​​of the density and size of the screen.

Afterword

The article does not cover practical matters related to testing applications on devices with different characteristics of screens, I think it's highlight in a separate post.