views:

293

answers:

1

The Android docs indicate that I should ship with different icons for different resolution phone screens. http://developer.android.com/guide/practices/screens_support.html#qualifiers

  • res/drawable-ldpi/my_icon.png // icon image for low density
  • res/drawable-mdpi/dpi/my_icon.png // icon for medium density
  • res/drawable-hdpi/my_icon.png // icon image for high density

This does not work when compiling with Eclipse for the Android. Does anyone has an example of a manifest file that works for multiple resolution icons?

Thanks, Gerry

A: 

You should still refer to your icon as @drawable/my_icon

The Providing Resources section of the Developers Guide goes even further and shows how you can create more complex aliases, in the following section:

Creating alias resources

When you have a resource that you'd like to use for more than one device configuration (but not for all configurations), you do not need to put the same resource in each alternative resource directory. Instead, you can (in some cases) create an alternative resource that acts as an alias for a resource saved in your default resource directory.

Note: Not all resources offer a mechanism by which you can create an alias to another resource. In particular, animation, menu, raw, and other unspecified resources in the xml/ directory do not offer this feature.

For example, imagine you have an application icon, icon.png, and need unique version of it for different locales. However, two locales, English-Canadian and French-Canadian, need to use the same version. You might assume that you need to copy the same image into the resource directory for both English-Canadian and French-Canadian, but it's not true. Instead, you can save the image that's used for both as icon_ca.png (any name other than icon.png) and put it in the default res/drawable/ directory. Then create an icon.xml file in res/drawable-en-rCA/ and res/drawable-fr-rCA/ that refers to the icon_ca.png resource using the <bitmap> element. This allows you to store just one version of the PNG file and two small XML files that point to it. (An example XML file is shown below.)

Drawable

To create an alias to an existing drawable, use the <bitmap> element. For example:

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

If you save this file as icon.xml (in an alternative resource directory, such as res/drawable-en-rCA/), it is compiled into a resource that you can reference as R.drawable.icon, but is actually an alias for the R.drawable.icon_ca resource (which is saved in res/drawable/).

However you do not need to create aliases if you simply have different icons for different screen densities. The resource directory qualifiers shown in the table you link to are ldpi, mdpi, and hdpi. The qualifiers are used when locating alternative resources (emphasis added):

The hdpi qualifier indicates that the resources in that directory are for devices with a high-density screen. While the images in each drawable directory are sized for a specific screen density, the filenames are the same. This way, the resource ID that you use to reference the icon.png or background.png image is always the same, but Android selects the version of that drawable that best matches the current device configuration.

Stephen Denne
If I refer to my icon as @drawable, can I put different resolution icons into drawable-ldpi etc, and will the runtime find the correct resolution icon? Do you have a sample manifest file that you could post?
Gerry
@Gerry, yes. I updated my answer to include a whole of heap of the docs. Not entirely relevant to just locating alternative resources, but going even further and overriding the image for just some locales.
Stephen Denne