Android – How to alias an android bitmap to a drawable from another size (drawable-large-mdpi aliases to drawable-hdpi)

androidbitmapdrawablescreen-sizetablet

I'm working on adding support for tablet sized screens to my apps. I already have images in drawable-mdpi and drawable-hdpi for different density screens. My problem is with tablets like the Galaxy 7" which is a "large" screen but is still medium density. Part of my layout has 5 buttons across the width of the screen which are evenly spaced. On the large screen with mdpi graphics though the images are very small with tons of whitespace between them.

I would like to use larger graphics for the large layout to make them look appropriate as well as take advantage of the screen real estate. I have some double sized graphics in my hdpi directory that would work perfectly. As a test, I've copied all of the images from /res/drawable-hdpi into /res/drawable-large-mdpi and everything looked exactly as I want.

However, I don't want to bloat the size of my app by coping all of those images. I would like to just create aliases for each of them so that when /res/drawable-large-mdpi/button_0 is requested, it will actually use /res/drawable-hdpi/button_0 instead.

I've tried creating an xml bitmap but I don't know how to reference a drawable from a specific directory. Any help?

Contents of /res/drawable-large-mdpi/button_0.xml:

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

The error I get with the above is:

error: Error: No resource found that matches the given name (at 'src' with value '@drawable-hdpi/button_0_highlighted').

Best Answer

Move your button resource into the drawable-nodpi folder and rename it to button_0_hdpi.xml.

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

When using an XML alias you can not specify the qualifier. You had @drawable-hdpi and it needs to be just @drawable. You probably also need to make a second XML alias in the original location of you button bitmap.

Here is a good article on the method http://blog.evendanan.net/2011/03/Android-resources-and-device-fragmentation

Related Topic