How To Create Screen Size Independent Android Applications

Written by:  • Edited by: Simon Hill
Updated May 18, 2011
• Related Guides: Android | Android Applications | Sony Ericsson

Since Android 1.6, new screen resolutions are supported, how do we adapt our application to this new feature? Lets have a look!

Introduction

I have been working with Android since the first beta release of the SDK. At that time there was no more than one Android device on the market, the HTC Dream, with a screen size of 3.2 inches and a resolution of 320x480. Since then Android devices have started to grow. Both devices with small screens like the HTC Tattoo, with a screen size of 2.8 inches and a resolution of 240x320, and almost-laptop devices like the Acer One. It was easy for us, developers to create applications that looked fine in an Android device. Now, we have to be careful, our good looking application can be transformed into a badly formatted application with missing icons and odd views.

So, from now on, if we are developing using the Android SDK 1.6 +, we have to take care about the size of the screen, if we want to run our application in multiple devices!

First of all let's see some concepts we need to know before taking care of our images, views and pixels.

Some concepts

*Resolution. Well, this is not a very difficult concept, we are all very familiar with this word. So, the resolution of the screen, is the size (width x height) in pixels. As I said before, and the HTC Dream device has a 320 x 480 resolution screen.

*Screen size. This is normally measured in inches. Its the diagonal size of the screen. 4 inches is the screen size of the Sony Ericsson X10 (Rachel).

*Density. In the base of the Resolution, we have the Density of pixels in the screen.

Now, with these concepts in mind, lets see what Android can offer us to work with in terms of screen sizes.

In Android, we work with the screen size and its density. To simplify we can check all the screen configurations available in Android by looking at the following table:

screenshot 003
click to enlarge

(Source: Android Developers Home Page).

So, we have:

Screen Sizes: large, normal, small

Density: high (hdpi), medium (mdpi), and low (ldpi).

Here are some of the techniques we can use to create a screen-size independent application.

Resources folders

So, thinking a bit about screen elements, sizes, densities... we can arrive at the conclusion that the size of the screen affects the views, and the layouts we create, and the density of the screen affects the images we set in the layout.

One of the things we can do (there are many) is create layouts, views and images for every specific size and density. So, the resources directory could be something like:

res/layout/layout.xml

res/layout-small/layout.xml

res/layout-large/layout.xml

res/drawable-ldpi/icon.png

res/drawable-hdpi/icon.png

This way, in the “layout” directory, we put the layouts that are going to fit in a “normal” screen size, “layout-small” stores the same layouts but modified so they display on small screens properly. The same process is done in the “drawable” folders. In the “drawable-ldpi” and “drawable-hdpi” folders we store the images we use in our application, changing its density.

Manifest

In Android SDK 1.6, we have a new element in the manifest, the <support-screen> tag. With this tag we can say how many screen sizes our application supports. android:smallScreens, android:normalScreens, android:largeScreens and android:anyDensity.

This is a piece of the manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<supports-screens

android:largeScreens="false"

android:normalScreens="true"

android:smallScreens="true"

android:anyDensity="true" />

</manifest>

What are we saying here? The android:largeScreen with the false value, we are saying that our application doesn't support large screens, but supports normal and small screens, these are the ones with the true value.

And what about the android:anyDensity” parameter? Here is the meaning:

-If we put this value to true we are disabling the Android internal way to manage densities, and we are saying to the system that we manage the icons and their densities (so it's good to have the drawable folders and its density options enable, like I explained in the last section)

-If we put false, we are enabling the Android internal way to manage densities. What is this? Android auto-scales the icons and elements given to adjust them to the screen density.

Device Independent Pixels (dip)

This is another interesting resource we can use in our “screen-independent” application: when we add an element to our view, we can set the width and the height of the element.

<Button android:id="@+id/myButton"

android:layout_width="10px"

android:layout_height="10px"

android:layout_gravity="center"

android:textStyle="bold" />

This button has a height and width of 10 pixels. What would happen if we run our application (the app that contains this element in one of the views)...

...in a device with low density?

Because we have less pixel density in the screen, this button will appear bigger than the same element in a “normal density” device.

...in a device with high density?

The button will be smaller to the user's eyes, because we have more pixel density in our screen.

So, what can we do to make our element look the same in all devices? We can do it using the dip (Device Independent Pixels) instead of px

android:layout_width="10dip"

This way, the element will appear to be the same size in all devices.

Some advices

After going through all the resources we can use in our application to create the most “screen-size” independent, I'm going to give you some advice.

Don't use AbsoluteLayout in your views. With AbsoluteLayouts you can place the elements in the screen given by an absolute position: Place this button in the position x= 100px, y = 150px. This is DEPRECATED. Use instead RelativeLayouts. With RelativeLayouts you place the elements inside the layouts according to relative positions to the parent-view or other elements in the screen. Something like “Place this button below this image”.

If we use AbsoluteLayouts in our application, the User Interface will be an absolute mess when you run the App in other devices.

To test our application with multiple screen sizes and densities we can use the emulator. We can create a AVD (Android Virtual Device) with different resolutions.

AVD
click to enlarge

Ask, comment, interact!

Ask any questions you have about the article, I will try to answer as fast as I can. Comment on my code, my writing, maybe there is something that is missing or is not complete, just let me know!! In other words, interact!!

Follow up

If you want to know when new articles are released, subscribe yourself to the Google Android RSS. Otherwise, you can follow my research, articles and work in my professional twitter: jbeerdev


Comments

Showing all 17 comments
 
Richard L. Sep 21, 2011 12:56 PM
GUI and 9-Patch images
One can't build UI without having to deal with 9-patch images.

You can find free 9-patch PNGs here:
http://android9patch.blogspot.com/

Enjoy!
Jbeerdev Jun 6, 2011 9:20 AM
RE: How To Create Screen Size Independent Android Applications
Maybe you can use the application Draw 9-patch.

http://developer.android.com/guide/developing/tools/draw9patch.html

With this application you can re-use drawable files to adapt them in land and portrait.

Hope it helps.
Mike Jun 5, 2011 4:22 PM
How To Create Screen Size Independent Android Applications
Thank you for the article. I have been working on a simple game as a way of learning android programming. I have 2 devices that I use for debugging through the ADB, one is a Huawei Comet 2.75" (320x470,) the other a Pandigital Novel White 7" (600x800.) I am using the drawable-ldpi, mdpi, etc, and layout-small, normal, large and xlarge.

My question is this; How can I restrict port and land views to a specific drawable file without creating more graphics and placing them in drawable land and port? I don't want to create a bloated app.
sathish Apr 15, 2011 5:42 AM
Appreciating and need help
Hi,
this is sathish , android developer in a middle stage. i would like to thank you for your research and more helpful articles. i will follow your articles to learn new things to improve my skills also i need your help in this.

Regards
sathish
Anoop CH Mar 21, 2011 6:36 AM
Device and Display Independent Programming in Android
There are several ways to achieve Screen Independence..

* Use 9 patch images wherever possible
* Design Layout using dimension units as dp, sp and measurements(in, mm...etc).
* Use fill_parent and wrap_content wherever possible.
* Use resource along with qualifiers (Specifying resources for a group/type of device)
* Designing, scaling and fitting using custom coding for your program (Not recommended) Do read the following:

http://developer.android.com/guide/practices/screens_support.html (Take good look at testing, best practices more keenly)

Consider the below Book written By me, (Only If you find difficult to gather all the points)

* Easier for new Programmers for understanding Screen Independence

Read More here about this Book:

http://androidforums.com/classifieds/302013-nanobook-device-independent-programming-android.html

Go here to by the book:

http://schogini.com/shop/nano-ebooks/31-device-independent-programing-in-android.html


Thanks for Your Tutorial I gathered a few points as well..
John Aug 25, 2010 11:33 AM
How to know which layout?
I didnt understand how the software will pick each layout folder for different resolutions. We pick a different folder name and put the specific drawables on each... then what??
Nivedita Jul 28, 2010 1:50 AM
How To Create Screen Size Independent Android Applications
I am working on android 2.2, I developed an application which is having an Image View and a button view.
In HVGA both the image and button are visible, but in QVGA I could see only the image. Rest of the buttons that are present under the image view is not visible.

How do I manage multi-screen resolutions in Android ?
Is there any thing like maintaining Aspect Ratio of the image to adjust the size as well maintain clarity.

I tried placing the buttons above the image view and it worked properly for HVGA and QVGA.
Please let me know how can I handle the situation by placing the buttons below the image view.

I have done it in relative layout

Image width and height is fill_parent and wrap_content
Button width and height is wrap_content and wrap_content.

plz help
Mike Jul 1, 2010 4:07 PM
um
So, if I use device independent pixels, how do I tell it what device to use as a reference? Like, 10dip on a Nexus One will be different to 10dip on a Droid X, so how does it know in which direction to convert?
Satish May 19, 2010 1:44 AM
Good Article
Hi Jbeer,
Thank you for the good article.

I have a small suggestion.
It would be very helpful if your article can be a little more elaborate. For instance, when explaining on dip, the spread of pixels can be mentioned. If possible, screen shots demonstrating the UI element's appearance on ldpi and hdpi screens can be shown (when px units are used instead of dip , illustrating the problem).
Jbeerdev Feb 24, 2010 3:52 AM
RE: How To Create Screen Size Independent Android Applications
Hi Sandroid

According to Google Android documentation, screen supports are what you can find in that table. Maybe now there are no support to smaller screens, but who knows in the future...
Sandroid Feb 23, 2010 5:55 PM
Screen size
The list of supported screens has 2.6" as the minimum. Would android work on screens smaller than 2.6" ( with 320X240 resolution)?
Usman Feb 2, 2010 6:38 AM
RE: How To Create Screen Size Independent Android Applications
Thanx Let me email it.
usman Feb 2, 2010 6:27 AM
RE: How To Create Screen Size Independent Android Applications
I am using sdk1.5 because this is the minimum requirement for my app. and i am using HTC Magic for testing and it has firmware 1.5, but when i make an emulator with large resolution i cannot see it right..
so tell me how can i show u my design layout ?
Jbeerdev Feb 2, 2010 6:14 AM
RE: How To Create Screen Size Independent Android Applications
So, I need more information about your environment:

What SDK are you using? What device are you using for testing ?

The problem, as I can understand, is that you can not see the screens in a right way with different devices, is that? Maybe if you provide us your layout, we can have a look at it.
usman Feb 2, 2010 6:05 AM
Hey Jbeer
Hey I am using relative layouts but not helping me so do you have any simple example ? which can help me in this matter. i have tried at first i was using linearlayouts but now i have converted each layout into relative layout and have given all the margins in "dpi" but still no vain..

Thanx
Usman
Jbeerdev Feb 2, 2010 6:01 AM
RE: How To Create Screen Size Independent Android Applications
Hi Usman

Using SDK 1.6 + you can use the screen-support. If you are working with 1.5... I can give you just one advice when creating screens (to make them the "most-able-screen-support") and it is: Use RelativeLayouts in your xml code.
Usman Feb 2, 2010 5:50 AM
Help
hey ,

thanx this is a pretty helpful article you've wrote but can you provide me with one screen that fits all the resolutions as i have to make it available for 1.5,1.6,2.0 so i cannot use screen-support tag in manifest. will be really grateful if you could help.

Thanx
Usman
 
blog comments powered by Disqus
Email to a friend