Android Interview Questions - SPLessons

Android multiple screens

Home > Lesson > Chapter 15
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Android multiple screens

Android multiple screens

Android multiple screens explains how to build an application that fits different screen sizes and densities. Though the system performs scaling and resizing to make your application work on different screens, you should optimize your application for different screen sizes and densities. Doing so will enhance the user experience and your users will strongly believe that your application was actually designed for their devices. Before learning how to support multiple screen sizes and densities, it is important to understand some terms such as:
Actual size measured as the screen's diagonal. To make it simple, Android groups all actual screen sizes into four generalized sizes: small, normal, large, and extra-large.
The quantity of pixels within an area of the screen, usually referred to as dpi (dots per inch). For example, a "low" density screen has fewer pixels within a given physical area, compared to a "normal" or "high" density screen. Android groups all actual screen densities into six generalized densities: low, medium, high, extra-high, extra-extra-high, and extra-extra-extra-high.
The orientation of the screen from the user's point of view. This is either a landscape or a portrait, meaning that the screen's aspect ratio is either wide or height, respectively. Keep in mind that not only do different devices operate in different orientations by default, but the orientation can change at run time when the user rotates the device.
The total number of pixels on a screen. When adding support for multiple screens, applications do not work directly with resolution; applications are concerned only with screen size and density as specified by the generalized size and density groups.
A virtual pixel unit that you should use when defining UI layout to determine the layout dimensions or the position in a density-independent way.
How to Support Multiple Screens:
  1. By declaring the screen sizes your application supports, you can ensure that only those devices with the screens you support can download your application. Declaring support for different screen sizes can affect how the system draws your application on larger screens—whether your application runs in screen compatibility mode.
  2. To declare the screen sizes that your application supports, you should include the <supports-screens> element in your manifest file. [code lang="xml"] [/code]
  3. Provide different layouts for different screen sizes. The way we place an object onto the screen is an aspect that requires consideration. It has to do with the resolution because we can have more or less space to place our components. Android provides a smart way to handle different layouts for different screen sizes. If we want to support all these screen sizes, we have to map these android names into the directory structure inside the res directory. The convention used by android is to add these names at the end of the layout word.
  4. As you can see in the image, each directory contains the same elements with the same names. The only thing that changes is the content of each element, which suggests that you need to just adjust the textSize or marginTop element. Try to set the width and height attributes value using wrap_content and match_parent, and, try to avoid the usage of dp.
  5. Provide different bitmap drawables for different screen densities. Android scales your bitmap drawables (.png, .jpg, and .gif files) and Nine-Patch drawables (.9.png files), so that they render at appropriate size on each device. You have to set different image resolutions for different screen densities.
Density changes the way image looks on various devices. An image looks right on one screen and looks too small in a different device with a higher density. This issue needs consideration. Android groups devices into categories based on their screen density. A set of generalized densities: To create alternative bitmap drawables for different densities, you should follow the 3:4:6:8:12:16 scaling ratio between the six generalized densities. For example, if you have a bitmap drawable that is 48x48 pixels for medium-density screens, all the different sizes should be: Android also has tvdpi support which is 1.33*mdpi. Like we did for layouts, we can build a directory structure under the directory res that maps these names. This time, instead of a layout, we use a drawable. So we will have: