danca
danca

Reputation: 509

Android image sizes

Im getting started with android development and i have a few questions about supporting different screen sizes. I have read all about it here but still dont get how to use different sized images for different screens. My situation is that I have put an image in res/drawable/hdpi which will be picked up when running on my samsung infuse 4g as well as on my galaxy 10.1 tablet. Because they have the same density class the image will have roughly the same physical size on both devices. And this is the problem, obviously I want the image to be way bigger on my tablet but I have yet not found a way to do this.

I did look at another classification whith small, medium and large but that seemed only to be applicable to layouts and not image resources.

Any pointers would be appreciated.

Thanks!

Upvotes: 5

Views: 5301

Answers (3)

damienix
damienix

Reputation: 6763

After putting images into for example

/res/layout-480x800` - if you know resolution
/res/layout-w480dp - if you know witdh
/res/layout-h800dp - if you know height

Android automaticly choose picture for you. Just put picture in resolutions but with the same name into this subfolders. Then just use (without spacyfing qualifiers):

android:src="@drawable/background"

or

imageView.setImageResource(R.drawable.background);

The order of precedence of qualifiers can be found in documentation (Table 2)

Upvotes: 1

hackbod
hackbod

Reputation: 91331

Those two devices don't have the same density. The Samsung Infuse is hdpi, the Galaxy Tab 10.1 is mdpi.

Further, you don't generally want the image to be bigger on the tablet. Consider -- if I have a list of contacts in my application, I want them to be the same height on the phone vs. the tablet because there is no use making them taller (that height is sufficient to display the text and be able to tap on it with my finger), but rather that I want to do is use the extra space to be able to display more of them.

If you really do want an image that is larger on the tablet than on the phone, you are going to need to explain more about what you are actually trying to accomplish. For example, maybe this is a background image? In that case maybe what you are looking for is to just have the image stretch to fill the screen. (And be aware that doing background images on Android is tricky because you do need to deal with a wide variety of screen sizes you will be fitting. To avoid making yourself go insane, background images should generally be very abstract so that stretching works well on them. Look at the background images used in Android 3.0 and 4.0 for the default themes as an example.)

If you are writing a game, this is a very different world, and you should consider whether you want to use density based resources at all. Generally games will have resources that are independent of density, and just have their game playfield fill the screen and are happy with the result being something larger on a tablet.

Upvotes: 7

Nick Betcher
Nick Betcher

Reputation: 2046

Tablets are usually xhdpi, not hdpi. So, for your Samsung Infuse, place the image in:

/res/drawable-hdpi/

And for the tablet, place it into:

/res/drawable-xhdpi

When setting the image resource via

android:src="@drawable/image"

or

imageView.setImageResource(R.drawable.image);

It will pick the appropriate image based on the density of the device.

Upvotes: 0

Related Questions