Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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').
like image 489
Kenny Wyland Avatar asked Feb 18 '11 21:02

Kenny Wyland


2 Answers

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

like image 141
theJosh Avatar answered Sep 29 '22 16:09

theJosh


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 =(.

like image 25
FoamyGuy Avatar answered Sep 29 '22 16:09

FoamyGuy