29

Extract from Android Developer Guide link above:

320dp: a typical phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc).
480dp: a tweener tablet like the Streak (480x800 mdpi).
600dp: a 7” tablet (600x1024 mdpi).
720dp: a 10” tablet (720x1280 mdpi, 800x1280 mdpi, etc)

So i got graphics(images) at resolution 320 pixels per inch from designer in these dimension only

480x800 hdpi

720x1280 mdpi

800x1280 mdpi

I am confused which size of images should be placed in mdpi folder, hdpi folder and xhdpi folder. I want to make one application which can work on most android phones and tablets ?

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
Muhammad Irfan
  • 1,447
  • 4
  • 26
  • 56

4 Answers4

38

You can create different graphic objects for use at different pixel densities. Android treats mdpi (160 pixels/inch) as the base density. So for mdpi devices, 1 dp = 1 pixel. At higher densities, there are more pixels per inch (240 for hdpi, 320 for xhdpi). Android attempts to make graphic images occupy the same physical dimensions on the screen regardless of the device pixel density. So if all it finds is an mdpi resource, and the device is hdpi, it will scale the graphic by 240/160 = 150%, and it will double the size of the graphic for xhdpi.

If you don't want this automatic scaling (which can make graphics look poor), you can simply supply your own version of graphic resources for use at higher densities. These graphics should be of the same size that Android would scale an mdpi resource.

Note that the pixels/inch that was stored in the image file has nothing to do with this. It's all based on where you put the graphics files in the resources directory for your project. Any graphics placed in res/drawable are assumed to be properly sized for mdpi displays, as are graphics placed in res/drawable-mdpi. Image files that it finds in res/drawable-hdpi are assumed to be properly sized for hdpi displays, etc. When your program runs on a particular device, Android will first look for a graphic that matches the display density of that device. If it does not find one but instead finds one for a different density, it will use that and automatically scale the image based on the above rules.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • As you said "Image files that it finds in res/drawable-hdpi are assumed to be properly sized for hdpi displays, etc " so I will put 480x800 hdpi in res/drawable-hdpi ?? – Muhammad Irfan Aug 01 '12 at 06:51
  • Also do i need to get change the pixel per inch ratio for all the images of size 480x800 as they are placed in res/drawable-hdpi folder so is it compulsory that their resolution(pixel per inch) must be 240 dpi ?? as I have all these images in 320 dpi ? OR dpi really does not matter here ? – Muhammad Irfan Aug 01 '12 at 06:56
  • 1
    @MuhammadIrfan - You would put the image in `res/drawable-hdpi` if it was the right size for a display at 240 pixels/inch. You should be aware that devices can have different sizes and still be hdpi. Also, devices can be the same size and have different pixel densities. This unfortunate truth is called "device fragmentation" and is part of why Android programming can be complex. Are you trying to exactly fit an image to the size of the display? – Ted Hopp Aug 01 '12 at 06:58
  • 1
    @MuhammadIrfan - The dpi of the images is irrelevant. Regardless of what dpi is stored in the images by Photoshop, Gimp, or whatever tool you're using, Android ignores it. The only factor Android considers is what directory the image are placed in. – Ted Hopp Aug 01 '12 at 07:00
  • @MuhammadIrfan - The documentation that best explains all this is [Supporting Multiple Screens](http://developer.android.com/guide/practices/screens_support.html). – Ted Hopp Aug 01 '12 at 07:02
  • Yes, I am trying to exactly fit the image to the size of the display. – Muhammad Irfan Aug 01 '12 at 07:14
  • 1
    @MuhammadIrfan - I think the best you can do here is use the `drawable-large`, etc. directories. If you want to avoid scaling based on display density, use `drawable-large-nodpi`, etc. If you do that, then Android will pick the image that best matches the screen size and then will scale to the actual pixels available to your app (which may not match the display size). Read [Providing Resources](http://developer.android.com/guide/topics/resources/providing-resources.html) for more info. – Ted Hopp Aug 01 '12 at 07:21
  • @TedHopp hey i have same problem of image being picked up by my three emulators:240x320_120dpi(LDPI), 320x480_160dpi(MDPI) and 480x800_240dpi.. i have placed an image with same name as logo.png in my all three folder:drawable-ldpi, drawable-mdpi and drawable-hdpi.. but all three emulator picks up image at drawable-mdpi.. Can you please help me..its been two weeks i have been searching for this solution.. – Suraj G Jan 05 '13 at 11:19
  • @SurajG - You need to set the display scale that the emulator will use when you start the emulator. See the [emulator documentation](http://developer.android.com/tools/help/emulator.html) on the `-scale` and `-dpi-device` command switches. (You can also set the scale using the dialog box that comes up when you start an emulator from within Eclipse.) – Ted Hopp Jan 05 '13 at 23:36
  • @TedHopp - hey thnx... but i finally got the solution for picking up image from particular resource folder... For all who face this problem .. **always mention in your manifest file the supporting device always ensure that what is your folder name.. Suppose you have as ldpi mdpi and so on; than dpi of images is very imp.. and if it is small large and so on; size is very imp.. according to mee it is better to give small large and so on in layout as well as drawable; because it completely make you independent of dpi..** – Suraj G Jan 06 '13 at 15:31
  • I am working on a app which includes a lots of images. If I supply all ldpi,mdpi,hdpi,xhdpi images it is increasing apk size very much. So if I supply only xhdpi images and let android os re-size it for hdpi,mdpi,ldpi will it be a good idea ? As in tihs way my apk size will be less too. – Pankaj Kushwaha Sep 02 '13 at 02:46
  • @PankajKushwaha - It all depends on how good a job Android does on scaling your images. Some images it handles quite well; with some it can botch the job quite badly. Its performance depends heavily on the nature of the images; the only way to tell is to test on various devices with different densities. If you're satisfied with the results, then supplying xhdpi only may be a good strategy. However, you should be aware that older versions of Android do not recognize xhdpi folders; in those cases the resource may not even load. I've not experimented with this, so I don't know. – Ted Hopp Sep 02 '13 at 03:47
  • Thanks for clarifiying!! :D I also used **this [table](http://developer.android.com/guide/practices/screens_support.html#testing)** as a reference. – KarenAnne Sep 20 '13 at 05:23
  • So if an image in hdpi folder is good for a hdpi large size device, but not too big for another hdpi device which has a small screen size, how would android sort that issue or if we have control over it too ? – Ahmed Jun 21 '16 at 01:08
  • 1
    @Ahmed - You have control over that as well. You can add a size qualifier (`-large`, `-small`, etc.) to the resource directory, optionally along with other qualifiers like density. For details, including the required order of the qualifiers, consult the docs (especially [Providing Resources](https://developer.android.com/guide/topics/resources/providing-resources.html) and [Supporting Multiple Screens](https://developer.android.com/guide/practices/screens_support.html)). – Ted Hopp Jun 21 '16 at 05:31
11

When you request a resource for which you provide alternatives, Android selects which alternative resource to use at runtime, depending on the current device configuration. To demonstrate how Android selects an alternative resource, assume the following drawable directories each contain different versions of the same images:

drawable/
drawable-en/
drawable-fr-rCA/
drawable-en-port/
drawable-en-notouch-12key/
drawable-port-ldpi/
drawable-port-notouch-12key/

And assume the following is the device configuration:

Locale = en-GB 
Screen orientation = port 
Screen pixel density = hdpi 
Touchscreen type = notouch 
Primary text input method = 12key

By comparing the device configuration to the available alternative resources, Android selects drawables from drawable-en-port.

The system arrives at its decision for which resources to use with the following logic:

enter image description here

Ref : How Android Finds the Best-matching Resource

Other References : Density independence , Providing Alternative Resources and Best Practices

And I will say that you should read complete page Supporting Multiple Screens, I don't think nothing will be better documentation than it...

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
3

I'm confused myself with all the screen size fragmentation but the basics are: 1. You need to create various folders under layouts to work with your images 2. Images will exist in the drawables folders also under various folders. 3. You should have a basic /layout and /drawable folder to accompany non-specific folders 4. Work from xhdpi then scale images down!

Examples for specific screen folders: /layout-hdpi /layout-xhdpi /drawable-hdpi /drawable-xhdpi

From what I know: 480 x 800 is hdpi (older phones eg S2, HTC Desire etc) 720 x 1280 is xhdpi (new phones eg S3, Galaxy Nexus etc)

Basically, Depending on the phone, android will grab resources from the necessary folder and if there is none then it will grab from the main '\layout' or '\drawable' folder. For example, the app running on a Galaxy Nexus will grab resources from '\layout-xhdpi' if the folder exists.

karlstackoverflow
  • 3,298
  • 6
  • 30
  • 41
  • 1
    According to the [Android dashboard](http://developer.android.com/about/dashboards/index.html) ldpi and mdpi devices represent 23.2% of the devices that accessed Google Play within the last two weeks. Also, screen size and pixel density are independent variables. For instance, there are 480 x 800 devices that are mdpi. – Ted Hopp Aug 01 '12 at 05:42
  • 1
    I was gonna google this but really cbf lol. Thanks though, its a lot more than I thought. But in a years time, that number will probably be half or less. Also note, some phones are considering 1080p resolutions which I think is stupid...(I just converted my app from hdpi to xhdpi and it was a beach lol) – karlstackoverflow Aug 01 '12 at 05:45
0

yes, you can make one app, but was need creating folders: /res/drawable, /res/drawable-mdpi, /res/drawable-hdpi and add content for all screen sizes

mbelsky
  • 6,093
  • 2
  • 26
  • 34