How can I alias an android bitmap to a drawable from another size (drawable-large-mdpi aliases to drawable-hdpi)
Asked Answered
G

2

15

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').
Gallivant answered 18/2, 2011 at 21:57 Comment(2)
Hi kenny how did you solve that problem ?????.I have same problem please help mePackhorse
I could never get the aliasing to work. I just had to copy all of the images into the drawable-large-mdpi directory.Gallivant
H
3

this page talks about the different modifiers you can use on resource folders. It seems to indicate that the order of precedence is such that screen size(small, med, large, xlarge) is higher than density(ldpi, mdpi, hdpi). I would think that this means if you renamed your drawables-hdpi folder to drawables-large-hdpi even though the Galaxy tab has a medium density it will still use the drawables from this folder because it has a large screen.

Edit: I just tested this out, it does solve your problem one way. They images inside the drawables-large-hdpi folder to show up on the Galaxy tab when running the app. But unfortunately adding the large qualifier makes it so they don't show up on medium sized screens with hdpi densities. Its looking like you might have to make separate folders and have 2 copies of your large resources if you want to get this functionality =(.

Heyer answered 18/2, 2011 at 23:44 Comment(5)
That is an interesting thought and I'll certainly keep that in mind for future problems, but I definitely want to figure out a way to alias either a whole directory or the individual drawables within the directory to another directory with specific modifiers.Gallivant
Thanks for the update. I'm going to give it a couple of days for other answers, but I'll accept this one if no one else has the solution.Gallivant
Are you sure that the Galaxy Tab is considered an mdpi device? I am playing around a bit with the resources of one of the apps that I am working on and by default it is taking its drawables from the drawable-hdpi folder. According to Wikipedia page for the galaxy tab it has 260 dpi. And according to the chart on this page: developer.android.com/guide/practices/screens_support.html hdpi starts around 200dpi. Are you sure that your galaxy is taking images from your mdpi folder?Heyer
I'm using the emulator, but of course the emulator is using whatever density I set. I found some web page that gave the density incorrectly (I should have looked at wikipedia). The problem still stands though for the Archos 10.1. I had a user contact us and send us a screenshot which looks exactly like my large screen but mdpi emulator. When providing larger images (copies of the hdpi images) in the large-mdpi folder it seems to work... but of course that is in the emulator. I'll have to do some more research and figure out what is going on. Thanks for your help.Gallivant
The Galaxy tab is documented to be an HDPI device, and returns such at runtime, too. You can read all about it on this official Google post: goo.gl/qQupA It's also defined as a "large" device -- so "large-hdpi" compared to the Xoom, which is xlarge-mdpi.Cerebral
W
19

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

Watchful answered 28/6, 2011 at 18:6 Comment(3)
When I get a free moment, I'll give this a try!Gallivant
Just worked this one out myself, came here to check for a better way and here's the same. UpvotedHagiocracy
IMO this should be the accepted answer, since considers what the OP asked: use aliases to solve the problemArtina
H
3

this page talks about the different modifiers you can use on resource folders. It seems to indicate that the order of precedence is such that screen size(small, med, large, xlarge) is higher than density(ldpi, mdpi, hdpi). I would think that this means if you renamed your drawables-hdpi folder to drawables-large-hdpi even though the Galaxy tab has a medium density it will still use the drawables from this folder because it has a large screen.

Edit: I just tested this out, it does solve your problem one way. They images inside the drawables-large-hdpi folder to show up on the Galaxy tab when running the app. But unfortunately adding the large qualifier makes it so they don't show up on medium sized screens with hdpi densities. Its looking like you might have to make separate folders and have 2 copies of your large resources if you want to get this functionality =(.

Heyer answered 18/2, 2011 at 23:44 Comment(5)
That is an interesting thought and I'll certainly keep that in mind for future problems, but I definitely want to figure out a way to alias either a whole directory or the individual drawables within the directory to another directory with specific modifiers.Gallivant
Thanks for the update. I'm going to give it a couple of days for other answers, but I'll accept this one if no one else has the solution.Gallivant
Are you sure that the Galaxy Tab is considered an mdpi device? I am playing around a bit with the resources of one of the apps that I am working on and by default it is taking its drawables from the drawable-hdpi folder. According to Wikipedia page for the galaxy tab it has 260 dpi. And according to the chart on this page: developer.android.com/guide/practices/screens_support.html hdpi starts around 200dpi. Are you sure that your galaxy is taking images from your mdpi folder?Heyer
I'm using the emulator, but of course the emulator is using whatever density I set. I found some web page that gave the density incorrectly (I should have looked at wikipedia). The problem still stands though for the Archos 10.1. I had a user contact us and send us a screenshot which looks exactly like my large screen but mdpi emulator. When providing larger images (copies of the hdpi images) in the large-mdpi folder it seems to work... but of course that is in the emulator. I'll have to do some more research and figure out what is going on. Thanks for your help.Gallivant
The Galaxy tab is documented to be an HDPI device, and returns such at runtime, too. You can read all about it on this official Google post: goo.gl/qQupA It's also defined as a "large" device -- so "large-hdpi" compared to the Xoom, which is xlarge-mdpi.Cerebral

© 2022 - 2024 — McMap. All rights reserved.