Android Style and Android Screen Size

This article details about the style ,screen size  and resolution about Android based application. The terms that are used to describe the screen characteristics for android are also explained.

Android Style

A style means we have to specify the looks and approaches of android application in external files and these styles can be assigned to the complete Applications like Activities or Views. In this approach we set common attributes once and after that we change the appearance in one central place.

Eg: –   The style.xml file will be created in the /res/xml folder.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="text">
<item name="android:padding">4dip</item>
<item name="android:textAppearance">?android:attr/textAppearanceLarge</item>
<item>#000000</item>
</style>
<style name="layout">
<item>#C0C0C0</item>
</style>
</resources>

Android Screen size

Androids never to support fixed sized mechanism because the android devices are totally different in the resolution and density. The basis unit of screen size measurement is in terms of “dp” or “dip”. “dp” is the base line of an android device.

Eg: –

320×480 with 160dpi (dpi stands for dots per inch) and this is the size of the first android device (G1). This size also known as mdpi (mdpi stands for medium dots per inch).

We define the size in “dp”, Android will automatically scale it, depending up on the device. On an mdpi device “dp” will be equal to pixel but it will be smaller on an ldpi (approx. 120dip) and larger on a hdip(approx. 240dpi) device, so that it always occupies the same physical space.

Example of “dp” is layout files.  We can calculate the right amount of pixels for a dimension specified in dp by using the following code.

Eg:

public int convertToDp(int input) {
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
return (int) (input * scale + 0.5f);
}

Android divides the range of actual screen sizes and densities int the following ways.

  1. A set of four generalized sizes:- small, normal, large and xlarge.
  2. A set of four generalized densities:- idpi(low), mdpi (medium), hdpi (high) and xhdpi (extra high).
Android Screen Size
Android Screen Size

Supporting multiple screen configuration

The android system allows a uniform development environment across devices and handles most of the work to adjust each application’s user interface to the screen on which it is displayed. At that time the system permits APIs that allows controlling our application’s UI for specific screen sizes and densities. The systems do scaling and resizing mechanism to create our application operations on different screens.

Terms and concepts or screen characteristics

1)      Screen size

The physical size measured as the screen’s diagonal. The screen sizes are four types

  1. Small:- At least 426dp x 320dp
  2. Normal:- At least 470dp x 320dp
  3. Large:- At least 640dp x 480dp
  4. Extra large:- At least 960dp x 720dp

2)      Screen density

The basic screen size measurement unit is dpi.

Eg:- a “low” density screen has fewer pixels within a given physical area, compared to a “normal” or “high” density screen.

The screen densities are categorized into four:-

a)       Low

b)      Medium

c)      High

d)     extra high

3)      Orientation

Orientation is based on the user’s point of view. Orientation is either landscape or portrait. That means the screen’s aspect ratio is either wide or tall. The orientation can change at runtime when the user rotates the device.

4)      Resolution

Resolution is the total number of pixels on a screen. In the case of multiple screen operation the application do not work directly with resolution and applications should be concerned only with screen size and density as defined by the generalized size and density groups.

5)      Density-independent pixel (dp)

To express layout dimensions or position in a density independent way we are using a virtual pixel unit that helps to defining UI layout. It is equivalent to one physical pixel on a 160 dpi screen. The system handles any scaling of the dp units at the run time and it is based on the actual density of the screen in use. px=dp*(dpi/160) expression helps the conversion of dp units to screen pixels.

Eg:-  On a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application’s UI, to ensure proper display of your UI on screens with different densities.

The Density independence operation works in two ways:

  1. The system scales dp units as appropriate for the current screen density.
  2. The system scales drawable resources to the appropriate size, based on the current screen density, if necessary

Without maintaining density independence a UI element seems physically larger on a low density screen and smaller on a high density screen. This leads to some problems in our application layout and usability. The following figure helps to show the difference between an application when it does not provide density independence and when it does.

The below figure shows Application without support for different densities, as shown on low, medium, and high density screens.

Application without support for different density
Application without support for different density

The below figure shows Application with good support for different densities, as shown on low, medium, and high density screens.

Application with support for different density
Application with support for different density