It's no secret that the number of devices on Android is large, they differ in hardware, screen size and quality, processor power, and others. Unlike iPhone programmers, who know for sure on which device their application will run, Android developers need to pay attention application compatibility with various devices.
This article focuses on the issue of application compatibility, primarily displaying applications on screens with different diagonals and resolutions.

First you need to understand the possibilities that provides Android to work with the screen.
')
Basic information
Screen size (screen size) - the physical size of the screen; predefined values: small, normal, large, extra large *.
Geometric coefficient (aspect ratio) - the ratio of the physical proportions of the screen (width to height); predefined values: long (for screens whose dimensions exceed standard screen sizes in width or height), notlong (for screens whose dimensions correspond to standard ones).
Density (density) - the distribution of pixels relative to the physical screen size. The value of the density of the pixel distribution is important because the same UI element, expressed in pixels for screens with a lower density, will seem larger than for screens with a high one. Predefined values for density: ldpi (low), mdpi (medium), hdpi (high), and xhdpi *.
An independent (from density)
pixel (density-independent or dp) is a “virtual” pixel that an application can use to draw UI elements. This pixel is the equivalent of a physical pixel on the screen with a density of 160 dpi. At run time, the Android OS draws the element in accordance with the formula pixel = dp * (density / 160), where density is the screen density.
It is also worth noting that the Android OS works with the screen resolution, through the screen density values (the developer does not have any means for working with resolution).
The figure below shows how the density and screen size of devices correlate with the predefined values of these quantities.

* Another point worth noting: the xdpi density value was added in Android 2.2 (API level 8), the xlarge screen value in Android 2.3 (API level 9)
Work with manifest and resource loading
Starting with Android 1.6, the <support-screens> tag has been added to the manifest, which is used to determine the class of devices on which the application can run. The attributes of the smallScreens, normalScreens, largeScreens, xlargeScreens tags correspond to the screen values defined above and can be true or false. The default attribute values vary depending on the version of Android you are using (more detailed information can be found
here .). When an attribute value is set to true, the Android OS receives a signal that the application is compatible with the appropriate screen type and does not use additional tools for compatibility (which happens when the value is false). It is also worth noting that these tools (functions) work only for compatibility with large screen sizes (so if the normalScreen value is true, the others are false, the application will also be compatible with large and xlarge screens, but not compatible with small) . This tag is also used by the Android Market to filter applications.
For density, there is also an attribute - anyDensity, which also accepts values true / false. If the attribute value is true, Android OS does not use functions for compatibility with various screen densities. In this case, the application must use dp to draw the UI elements, or independently manage the calculation of dimensions for different densities. If the value is false, the Android OS includes functions for scaling elements according to the density of the screen.
Resource allocation
The Android OS also provides the means to determine the resources that will be used for specific screen sizes and densities. Resources are placed in the 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
Screen Compatibility Support
In short, the support mechanism can be described using the following steps:
- The Android OS retrieves the attribute values of the support-screens tag from the manifest.
- Resources are loaded for the appropriate screen size and density (this happens regardless of the data obtained in claim 1).
- In accordance with Clause 1, the Android OS includes / does not include features for compatibility.
- Produced drawing elements.
General recommendations for creating a compatible application
- Use wrap_content, fill_parent, dp values in layouts.
- Avoid using AbsoluteLayout.
- Use the methods of the ViewConfiguration class to obtain standard values for size, speed, time.
- Use different resources for different densities and screen sizes.
Afterword
The article does not cover the practical aspects associated with testing applications on devices with different screen characteristics, I think to highlight this in a separate post.
Main source:
developer.android.com