I understand that android choices drawables based on their location (-ldpi, -mdpi and so on). How should I organize my images, if I have two sets of drawables, one for mdpi and ldpi and another for hdpi and xhdpi?
-
have you tried putting one set on the mdpi and another on the hdpi? – renam.antunes Jan 12 '13 at 20:43
-
@renam.antunes: That won't work. If a device is xhdpi and no drawable is found in the `/drawable-xhdpi` folder, Android will search in the default `/drawable` folder and _not_ in the next-lower-dpi folder: "If no matching resource is available, the system uses the default resource and scales it up or down as needed to match the current screen size and density" from [here](http://developer.android.com/guide/practices/screens_support.html#support) – saschoar Jan 13 '13 at 09:25
1 Answers
For now, I didn't come across any possibility to group dpi folders. But you still got choices:
Dummy drawable:
Put your images into your normal /drawable
folder and name them like image1_low.png
and image1_high.png
. Now put a "dummy drawable" file image1_dummy.xml
into your /drawable-ldpi
and /drawable-mdpi
folders, containing something like this:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/image1_low" />
The same goes for the /drawable-hdpi
and /drawable-xhdpi
folders, but now with @drawable/image1_high
instead of @drawable/image1_low
.
Now you can refer to the name of the dummy, e.g. by
getResources().getDrawable(R.drawable.image1_dummy);
in your code to get the desired drawable.
Programmatical:
You're not restricted to using folders, there is also a programmatical approach (credits to https://stackoverflow.com/a/5323437/1140682):
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
switch(metrics.densityDpi){
case DisplayMetrics.DENSITY_XHIGH:
// assign your high-res version
break;
case DisplayMetrics.DENSITY_HIGH:
// assign your high-res version
break;
default:
// assign your low-res version
break;
}