Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force tablet to use xhdpi resources

Tags:

android

xml

I'm working on an application and i've provided all the resources from ldpi to xhdpi, but some tablets use hdpi (or tvdpi in the case of the nexus 7 i'm testing on) so when i open the app on a tablet everything is small, i would like to force them to use the xhdpi resources how can i do that?

I don't want to create separate files just for the tablets or the size of the app would increase, the xhdpi resources would work fine

enter image description here

On the phones everything is displaying properly, the tablets are the problem because they take small resolution assets for their big screens, so the actual size in inch( 4cm, measured with a ruler) is correct, but they actually look small making the overall design incorrect

like image 731
DoubleP90 Avatar asked Feb 25 '13 20:02

DoubleP90


1 Answers

Usually, downscaling is the best option (as opposed to up-scaling). If your images don't degrade or loose information when they are downscaled, put the 2x version of your images in the res/drawable-xhdpi folder. Android will scale these images down appropriately on lower density devices.

E.g. if you put a 64x64 pixel jpg/png in res/drawable-xhdpi, Android will show it as a 32x32 pixel on mdpi devices, as a 48x48 pixel image on hdpi devices and as a 64x64 pixel image on xhdpi devices.

If downscaling your images would deteriorate your them (e.g. you have 1 pixel lines in your image that would disappear when scaled down), then the only option is to add the variations of your images in the drawable-mdpi, drawable-hdpi, etc. directories.

Update after comments:

You'd want to have your images appear larger on larger screens (not on higher density screens). You could do this by still putting your images in the drawable-xhdpi folder only, but then do this:

E.g. you'll have this layout in res/layout with an ImageView that shows one of your images:

res/drawable-xhdpi/one_of_my_images.png

res/layout/somelayout.xml

....
<ImageView 
    android:layout_height="@dimen/image_height"
    android:layout_width="@dimen/image_width"
    android:src="@drawable/one_of_my_images"
    android:scaleType="fitCenter"
    ....
    />

res/values/dimens.xml

....
<dimen name="image_height">100dip</dimen>
<dimen name="image_width">150dip</dimen>
....

res/values-large/dimens.xml

....
<dimen name="image_height">150dip</dimen>
<dimen name="image_width">225dip</dimen>
....

res/values-xlarge/dimens.xml

....
<dimen name="image_height">200dip</dimen>
<dimen name="image_width">300dip</dimen>
....

As you can see, the larger the screen, the larger (in dip and therefore in inches) the image. You can experiment with different values for image_height and image_width in dimens.xml in res/values-xlarge-hdpi, res/values-xlarge-xhdpi, etc and see what looks best.

Update 2 after comments: If you want your own 'custom' scheme of loading the appropriate resource, put them id res/drawable-nodpi: E.g
res/drawable-nodpi/one_of_my_images_normal.png res/drawable-nodpi/one_of_my_images_large.png res/drawable-nodpi/one_of_my_images_xlarge.png

Then put in the appropriate drawable dir (res/drawable/, res/drawable-large, /res/drawable-sw360dp/, etc, whatever you can come up with) an xml file that indirectly references the correct resource from the nodpi directory:E.g.

res/drawable-xlarge-mdpi/my_images.xml

<resources>
    ....
    <item type="drawable" name="one_of_my_images">@drawable/one_of_my_images_large</item>
    ....
</resources>

This means that for extra-large mdpi screens, Android will load the 'one_of_my_image_large.png' from the drawable-nodpi directory.

like image 179
Streets Of Boston Avatar answered Oct 20 '22 05:10

Streets Of Boston