Handle Multiple Screen Sizes in Android

11 / Jul / 2014 by Himanshu Singh 6 comments

Android devices come in a variety of screen sizes and resolutions. That’s why handling the multiple screen size in android is most important.

TERMS AND CONCEPTS

Screen size : Actual physical size, measured as the screen’s diagonal.For simplicity, Android groups has four generalized sizes: small, normal, large, and extra large.

Screen density : The quantity of pixels within a physical area of the screen; usually referred to as dpi (dots per inch).For simplicity, Android groups has four generalized densities: low, medium, high, and extra high.

Orientation :  The orientation of the screen from the user’s point of view.In Android, This is either landscape or portrait.

Resolution : The total number of physical pixels on a screen.In Android, we do not work directly with resolution; applications should be concerned only with screen size and density.

How To Support Different Screen’s.

Basically we are mainly this things by three ways.
1. Explicitly declare in the manifest which screen sizes your application supports.
2. Provide different layouts for different screen sizes.
3. Provide different bitmap drawables for different screen densities.

1. Explicitly declare in the manifest which screen sizes your application supportss

To set up support for multiple device sizes in Android, add the <support-screens> element into the AndroidManifest.xml file. This field specifies which screen size support and which do not.

[xml]
<?xml version="1.0" encoding="utf-8"?>
<manifest ..>

<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens= "true"
android:anyDensity="true"
/>

<application… >
….
</application>
</manifest>
[/xml]

we can also define dp in the <support-screens> element like-

[xml]
<manifest >
<supports-screens android:requiresSmallestWidthDp="600" />
</manifest>
[/xml]

2. Provide different layouts for different screen sizes

As we design our UI for different screen sizes, we’ll discover that each design requires a minimum amount of space. So, each generalized screen size above has an associated minimum resolution that’s defined by the system. These minimum sizes are in “dp” units.

[java]
res/layout/my_layout.xml // layout for normal screen size ("default")
res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-xlarge/my_layout.xml // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation
[/java]

We can also define it on the basis of dp like –

[java]
res/layout/main_activity.xml // For handsets
res/layout-sw600dp/main_activity.xml // For 7” tablets(600×1024 mdpi).600dp wide and bigger.
res/layout-sw720dp/main_activity.xml // For 10” tablets (720×1280 mdpi).720dp wide and bigger.
[/java]

3. Provide different bitmap drawables for different screen densities

To set labels for different layouts, dimensions or different screen density resources, use the following resource directories:

Aug13_handling01

For example:

[java]
res/drawable-hdpi/myImg.png // bitmap used for high-density
res/drawable-mdpi/myImg.png // bitmap used for medium-density
res/drawable-ldpi/myImg.png // bitmap used for low-density
res/drawable-xhdpi/myImg.png // bitmap used for extra high density
[/java]

Handing Screen Density

We can develop our application to maintains the physical size of the interface by using match_parent and wrap_content values for android:layout_width and android:layout_height. This parameter does not specify size but adapts to the space available.

[xml]

android:layout_width = "match_parent"
android:layout_height = "wrap_content"

[/xml]

Bitmap scaling can result in blurry images. To prevent this, provide higher-resolution bitmaps for high-density screens and the system will use those instead of resizing the bitmap designed for medium-density screens.

A

Following are ways the Android system helps to achieve density independence:

1. Android considers dp [density-independent pixels] units as appropriate for the current screen density.

2. Android also accounts for scaled pixels (sp), which are scaled based on the user’s preference of font size (FONT_SCALE value in System. Settings).

3. Android scales the drawable resources to the suitable size, if alternative resources are not available, and it solely depends on the current screen density.

FOUND THIS USEFUL? SHARE IT

comments (6)

Leave a Reply to priyanka darbari Cancel reply

Your email address will not be published. Required fields are marked *