26

I have 4 different sizes for each of the icons I need to use in my app. The problem is My Nexus 7 (1280 x 800) and galaxy s2 (800 x 480) seem to use the resources in drawable-hdpi. How do I force the Nexus to use resources in drawable-xhdpi and then the 10 inch tab to use drawable-xxhdpi.

I have this in my manifest file

<supports-screens android:resizeable="true"
              android:smallScreens="true"
              android:normalScreens="true"
              android:largeScreens="true"
              android:xlargeScreens="true"
              android:anyDensity="true" />
Amanni
  • 1,924
  • 6
  • 31
  • 51
  • 2
    as other are pointing out, the problem is not how to force Android to run the way you want it to, but to for you to understand how resource selection works in android and use it correctly. I highly recommend you to read the official doc. – Teovald Apr 18 '13 at 10:21
  • My app wasn't using the right density (it was using mdpi where hdpi was available). Your code snippet helped me fix my problem, and may help others, so I have upvoted your question in the name of preservation. – 1owk3y Oct 17 '13 at 16:24
  • I think authors should update their answers for latest devices and new android apis. – Bhaumik Desai Jan 11 '17 at 07:42

3 Answers3

103

How do I force the Nexus to use resources in drawable-xhdpi and then the 10 inch tab to use drawable-xxhdpi?

You can't.

The qualifiers hdpi,xhdpi,xxhdpi describes the screen density of the device, not the size of screen. From the official doc

Screen density

The quantity of pixels within a physical 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. For simplicity, Android groups all actual screen densities into four generalized densities: low, medium, high, and extra high.

If you want to support tablets also, use large, xlarge qualifiers. Nexus 7 is a large-hdpi tablet(technically it's tvdpi, but takes images from hdpi). So if you want to put images for Nexus 7, make a folder named drawable-large-hdpi and put the images there.

Note: This is the special case for Nexus 7. Because even though Nexus 7 is a 7 inch tablet, it has resolution of 1280 * 800. So it's an hdpi device. But normal 7 inch devices have lower resolutions of 1024 * 600. So they are mdpi devices. So the drawable qualifier can change. (From my own experience, first put a folder drawable-large-mdpi for 7 inch devices and check it on Nexus 7. If there is no problem with images, you dont have to put another folder. Because if a particular folder is not present, Android will check for the nearest possible folder and optimize it for the device screen)

Now regarding the 10 inch tablets case, they are xlarge devices and their densities can change from mdpi to xhdpi(Nexus 10). But many have resolution of 1280 * 800 and they are mdpi devices.

The better practice is to put the following drawables

// for Phones
drawable-ldpi
drawable-mdpi
drawable-hdpi

//for 7 inch tablets
drawable-large-mdpi
drawable-large-hdpi(for Nexus 7)

// for 10 inch tablets
drawable-xlarge-mdpi
Renjith
  • 5,783
  • 9
  • 31
  • 42
  • Very good explanation, AndroidVogue got the right answer first but I think may help explain better for me and anyone else which similar problems. Great effort – Amanni Apr 18 '13 at 11:10
  • +1 for saving my day, explained better than Google developer's site..Great!! – Ravi K. Sharma May 10 '13 at 06:36
  • How about using just drawable-large and drawable-xlarge in case you don't need the distinction Nexus7 / other 7 inch tablets? – Yar Nov 14 '13 at 18:50
  • If you want only that, use only `drawable-large` and `drawable-xlarge`. There is no specific rule in using the qualifiers other than their order. – Renjith Nov 15 '13 at 03:26
  • I know this is a very old question but I have just one concern with the solution. If you have assets of around 2 mb in one folder then multiplying them would increase to a bigger apk size. Is there a workaround so that the images dont look sluggish and the apk size remains small as well when it comes to developing for both phones and tablets? – SoH Jan 21 '15 at 15:29
  • @SoH You can always use [Multiple APKs](http://developer.android.com/google/play/publishing/multiple-apks.html) – Renjith Jan 22 '15 at 02:43
  • 1
    @RKN Thanks for your answer. I am using Nexus 9 inch tablet so how to define the drawable folder for this one. Resolution - 2048 x 1536 – Satheesh Mar 07 '15 at 06:40
  • @Renjith Your explanation is worth reading...and helped me a lot.. plz let me know that apart from drawable folder how we have to maintain layout and dimens folder.... for these layout and dimens folder also we need to maintain different folders – Pragya Mendiratta Dec 11 '17 at 06:25
1
The problem is My Nexus 7 (1280 x 800) and galaxy s2 (800 x 480) seem to use the resources in drawable-hdpi

that depends upon device like nexus 7 has 240dpi which is a hdpi device it will take drawable resources from hdpi like this if you test the same in samsung tab 2 it will take the same drawable from mdpi because its a mdpi device

Abhijit Chakra
  • 3,201
  • 37
  • 66
1

Try to use Configuration qualifiers for your resources. This is the best practice. Like res/drawable-normal-hdpi-port/icon.png.

Ref Link: Configuration qualifiers

Thanks.

  • so I can have mdpi, hdpi and xhdpi for normal and then 3 more for large and x-large? – Amanni Apr 18 '13 at 10:19
  • @Amanni Not necessary. Create folder according to your needs. I mean what kind of devices your are targeting. for Nexus 7 you can create `res/drawable-large-hdpi` folder. It would use images from that folder. So now you have two `Hdpi` `Drawable` folder - one for `480x800 -> drawable-hdpi` and the newly created one for `Nexus 7 -> drawable-large-hdpi` tablet. –  Apr 18 '13 at 10:26