mdpi image is too small for 10" android tablet-ThrowExceptions

Exception or error:

I have drawables for each density as ldpi,mdpi,hdpi and xhdpi.

Problem:
Both 10″ tablet device (1280×800) and a handset device 320×480 uses medium density. I have an 35×35 mdpi image. But this image is too small for tablet whereas it is ok for handset.

I read relevant documents. But I guess I couldn’t understand sth significant. Is there anyway for tablet use xhdpi images or I wonder how to use bigger images for mdpi tablet.

How to solve:

You should add a second identifier large and/or xlarge to your drawable folders. Increase the size in drawable-xlarge-mdpi until you are happy with the result while the drawable-mdpi will be the same size as before.

This increases the app size, but it will fix your issue.

###

If you want to achieve this without increasing your app size, there is a way to let a high density screen and a large medium density screen use the same resource. You need to place the image you want to re-use in the ‘drawable-nodpi’ folder. This will stop the platform performing its own scaling when using it. For example, assuming you have a resource called ‘my_resource’, if you want the tablet-size screen to use your xhdpi resource, then move it out of drawable-xhdpi and rename it like this:

/drawable-nodpi/my_resource_xhdpi.png

Then in both the drawable-xhdpi and drawable-xlarge folders, create a file called my_resource.xml which looks like this:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/my_resource_xhdpi">
</bitmap>

Now when you use @drawable/my_resource, the xhdpi version will be used by xhdpi screens and xlarge screens, and you only have to maintain one version of the image. I use this technique quite a lot and it works really well. Hope that helps!

###

These are defined as common practice:

Devices:

drawable-ldpi/xyz.png          low resolution devices 320x240 (now a days no phone comes with this resolution)
drawable-mdpi/xyz.png          medium resolution devices 480x320
drawable-hdpi/xyz.png          high resolution devices 800x480
drawable-xhdpi/xyz.png         Extra high resolution devices 1280*720
drawable-xxhdpi/xyz.png        Extra Extra high resolution devices 1920x1080

Tablets:

drawable-large-mdpi/xyz.png    7” Tablet (600x1024 mdpi)
drawable-xlarge-mdpi/xyz.png   10” tablet (720x1280 mdpi, 800x1280 mdpi, etc).

###

I include some resources in drawable-xlarge, and drawable-large folders, in addition to the drawable-hdpi folders. This works for me.

Another alternative, is to scale an image in code directly, which I do with the following code:

Drawable scaledIcon = new BitmapDrawable(Bitmap.createScaledBitmap(myImage, pxInt, pxInt, true));
myImageView.setBackgroundDrawable(scaledIcon);

It is not a good idea to scale a lot of images in your code (using this method), as I think it is resource intensive to modify the images on the fly like this. I do use this in some cases, where the image I am using may not be a standard size (and I want it to fit right always).

Leave a Reply

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