Force tablet to use xhdpi resources - android

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
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

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.

Related

Resolution in Android app

When I create my Android app i create 6 drawable folders:
drawable-hdpi. // resolution 1.5 x
drawable-mdpi. // resolution 1 x
drawable-xhdpi. // resolution 2 x.
drawable-large. // resolution 2 x.
drawable-xlarge. // resolution 2.5 x.
drawable-xxhdpi. // resolution 3 x.
In every folder i put the same image with different size according to android device resolution and screen size.
Is this correct? or this is insufficient for my android app ?
When i make project in Android Studio it automatically generates the folder structure for drawable- i.e
-- res
| -- drawable-hdpi (for low density screens)
-- drawable-mdpi (for medium density screens)
-- drawable-xhdpi (for high resolution screens)
-- drawable-xxhdpi (for extra high resolution screens)
The drawable-xxxhdpi qualifier is necessary only to provide a launcher icon that can appear larger than usual on an xxhdpi device. You do not need to provide xxxhdpi assets for all your app's images.
I think that the above is more than sufficient for an android application. And actually xxhdpi is more than enough for a high res tab or phone. Please go through Supporting Multiple Screens.
First, it's not necessary to put all the same images with different size in every folder. If you put the full set of images in drawable-xxhdpi folder, the system will re-size the image for other density automatically. Sometimes, some image may lost important details while re-sizing, then you should redesign a proper image for the density and put in the corresponding folder, not ALL images.
Second, xlarge/large/normal/small are the size of screen, ldpi/mdpi/hdpi/xhdpi/xxhdpi are the density of screen, the resolution is the combinations of size and density.
Finally, you may prepare just one set of image in res/drawable-xxhdpi folder, then design different layout for different screen size in res/layout-*** folders, just set different dp in width/height/margin etc..
Reference: https://developer.android.com/guide/practices/screens_support.html

Android devices with different height takes same layout folder

I have a Micromax AQ5000 with Screen Resolution- 1280*720 pixels and Motorola Moto G XT1033 with resolution 720 x 1280 pixels.I have a layout folder named layout-sw360dp which I designed for devices like Samsung s4,s3,Micromax canvas etc but this Motorola device is also using the same layout and this creates the images displayed as distorted in it.
How can I create a folder for the small device(Moto g) I tried layout-xhdpi but it doesn't work how can I name layout with height and width.
Well you are right in some sense android should take layout dependent on different densities but some mobile do not fall under specific density. Therefore android will pick up default layout from layout directory.
to support multiple screen resolution provide different layout for different screen sizes, you can make following directories in res directory like this
layout-hdpi
layout-mdpi
layout-xhdpi
layout-xxhdpi
layout-w320dp-h408dp
layout-w480dp-h800dp
layout-w480dp-h854dp
layout-w720dp-h1280dp
layout-w1080dp-h1920dp
when you provide layout in all this directories you will give multiple screen support for different sizes as well
layout-w1440dp-h2560dp
Use "dip" instead they will help you in debugging your layout as they will try to keep a coherent size to multiple screen resolutions,
<ImageView
android:id="#+id/avtar_animation_11"
android:layout_width="45dip"
android:layout_height="45dip"
android:src="#drawable/avtar011"/>
while supporting multiple screen when you give "dp" to dimensions, Actually android expects you to provide different values for different screen resolution. Lets say below is your imagview dimensions make few folders in res folder in your android project like these below
values-hdpi, values-mdpi, values-ldpi, values-xhdpi, values-xxhdpi
and in them make a dimens.xml file each of them and write
<dimen name="image_view_width">28dp</dimen>
<dimen name="image_view_height">28dp</dimen>
now that i have mentioned "dp" here instead of dip android wants me to keep track for different dimensions for different screen resolution, So i will change the image_view_width and image_view_height values are in separate values folders where dimens.xml is located. Make sure your dp values change as per your screen resolution you want your view to fit in.
<ImageView
android:id="#+id/avtar_animation_11"
android:layout_width="#dimen/image_view_width"
android:layout_height="#dimen/image_view_height"
android:src="#drawable/avtar011"/>
hard part is over now android will pick one of dimens.xml values depending on which screen your app is running, Voila now your layout rocks

Custom dimens.xml for mdpi devices - how it can be done?

My application have layout, which looks perfect at all devices, except devices with mdpi screen destiny. Activity just doesn't fit to screen on mdpi devices.
So I want to create special dimens.xml for these type of devices.
I created "values-mdpi" folder next to my "values" folder, created new dimens.xml into it and set dimens values for mdpi devices.
res/values/dimens.xml:
<resources>
<dimen name="logo_block_height">100dp</dimen>
</resources>
res/values-mdpi/dimens.xml:
<resources>
<dimen name="logo_block_height">50dp</dimen>
</resources>
And ImageView that uses that dimen:
<ImageView
android:layout_width="250dp"
android:layout_height="#dimen/logo_block_height"
android:id="#+id/font_logo" android:adjustViewBounds="false" android:src="#drawable/bg_font_logo"
android:layout_marginTop="20dp" android:layout_marginBottom="20dp"/>
The problem is that values now applied for all devices, not only for mdpi, but for hdpi, xhdpi, xxhdpi too.
I want it to work like that:
If device screen destiny is mdpi or lower - use values from values-mdpi folder
If device screen destiny is MORE than mdpi - use default "values" folder.
How it can be done?
So I came up with only one solution for that without creating values folder for each screen density.
When at least one "values" folder with destiny qualifiers created android selects the most suitable folder for device screen destiny.
This is the reason why it selects "values-mdpi" for everything that bigger or smaller than mdpi.
Also, like ataulm said, there are no sense use screen density qualifiers in that situations. I can have phone with 340x480 with 160dpi where content doesn't fit and Tablet with 1280x800 with 160dpi where there is too much free space.
I solved that by creating Values folder with minimal-height qualifiers.
I have folder "values-h480dp" with values for every device with minimum 480 dp availible height and put default values in it. In default "values" folder i put values for devices with small screen.
Now it works like I need:
If device have availible height more than 480dp - it uses values from "values-h480dp" folder.
If device have availible height less than 480dp - it uses values from "values" folder.
I sorry for my bad English and hope that this has helped someone.

Image size for different screen size and resolution

What I have In my project is a drawable folder in which I've placed all the drawables I need to resize some of them to do that I placed values folder:
values-normal-xhdpi
values-normal-mdpi
...
all the values that are in the xhdpi must be divided by 80/60=1.333
so that I get the same display for both devices...
why is it like that? when I divide it by 2 I get small buttons on the mdpi device;
and when I keep the dimensions the same I get very big buttons on the mdpi device;
Example:
in the values-normal-mdpi:
<!-- In the about the progragm activity the buttons -->
<dimen name="aboutproductimgwidth">73.5dp</dimen>
<dimen name="aboutproductimgheight">73.5dp</dimen>
<!-- the logo in the animation activity -->
<dimen name="logoimgwidth">162dp</dimen>
<dimen name="logoimgheight">162dp</dimen>
in the values-normal-xhdpi:
<!-- In the about the progragm activity the buttons -->
<dimen name="aboutproductimgwidth">98dp</dimen>
<dimen name="aboutproductimgheight">98dp</dimen>
<!-- the logo in the animation activity -->
<dimen name="logoimgwidth">216dp</dimen>
<dimen name="logoimgheight">216dp</dimen>
when I placed these values I've got the exact same display on both devices..
and that's what I need.
EDIT1
Even when I did it in another way, and that's by placing an image of 160px in the drawable-normal-xhdpi folder and another one with 80px in the drawable-normal-mdpi folder I've got different display on both devices; on the mdpi device I've got really large buttons and on the xhdpi device I've got a nice display why?!! although I did what they said I divided the image of xhdpi on 2;
EDIT2
sorry I placed some photos but can't keep them online for too much time.. but what I have in my display:
ONE DEVICE HAS A TEXT UNDER THE BUTTONS AND A TEXT ABOVE THE BUTTONS BECAUSE THE BUTTONS ARE TOO LARGE AND IN THE OTHER DEVICE I AM GETTING THE TWO TEXTS ABOVE THE BUTTONS BECAUSE THE BUTTONS ARE JUST AS BIG AS I NEED THEM TO BE.. ALTHOUGH I USED IN THE FIRST DEVICE BUTTONS 80PX BECAUSE IT'S A MDPI DEVICE AND IN THE SECOND ONE 160 PX BECAUSE IT'S XHDPI
If you did it properly, then the image in your drawables-mdpi should be 1/2 the size of the image in xhdpi.
What you're seeing on the screen is that a typical mdpi device is 320 px while a typical xhdpi is 720 px. So if you made two buttons, each 1/2 the screen size, then the xhdpi buttons should be 360px. Following "procedure" means half sized 180px buttons in mdpi. But on the mdpi screen 2 buttons would be 360px, and not fit. To get them to fit you would need to reduce them 30 px, or about 13.33%
Sounds like the screens you are looking at may not be typical screens, but the root of the problem is that you are adjusting the image size in both the drawables and in the values folders. You need to redesign your approach to handling different screen densities - pick one or the other, but probably not use both unless you have a very unusual use case.
You are having problems in setting the correct dpi of image for different drawable folders. If you want to automatically generate a image for all required sizes the please use http://android-ui-utils.googlecode.com/hg/asset-studio/dist/index.html.
Here you can upload your image or use text/clip art and it will provide you with a zip containing images of all dpi.
You need to add settings for different device sizes in your AndroidManifest.xml.
You can see that settings here.

Android supporting multiple screens with Images

I read a lot about supporting multiple screens problems in Android, but I didn't solve mines yet. If I understood well, there are at least 2 ways to show images on different screens and to make it look normal. First, to make many images (xhdpi, hdpi, mdpi, ldpi). Second, is to create layouts(big, medium, small and etc.) My question is: which method is better? Now, my problem. I took first method, create my image in different sizes and copied to folders according to image's size. I used DEVS BITMAP app to get size, which I want my image to look like for different screens. I'm getting perfect view when I look on the 7' screen, but the view gets terrible on 3' screen. I copied this code to my Manifest:
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:xlargeScreens="true" />
I think it isn't necessary, but I wanted to be sure I am doing everything good. Could anyone answer my question and solve my problem? I would appreciate your help! ;)
Create a new Project.
There is a layout folder ,values-sw600dp,values-dw720dp-land. In both these folders, there is a dimens.xml file. What is written in it is this :-
<resources>
<!--
Customize dimensions originally defined in res/values/dimens.xml (such as
screen margins) for sw720dp devices (`e.g. 10" tablets)` in landscape here.
-->
<dimen name="activity_horizontal_margin">128dp</dimen>
That mean folders with sw-720 dp are for 10 inch devices, similar 600dp are for 7 inch devices and default layout folders vary from 3 to 5 inch device.
layout-sw600dp -> This folder will have xml in PORTRAIL Orientation for 7 inch devices
layout-sw600dp-land -> This folder will have xml in LANDSCAPE Orientation for 7 inch devices. When you rotate your device, view will be formed from this folder
Based on the dpi/resoulution of your devices, you can create unlimited number of layout folders.
Similarly, there are folders for images called drawable ->hdpi ldpi mdpi xhdpi xxhdpi. Based on the resolution of devices, images are picked from their respective category.
xxhdpi will contain images with the highest resolution. So try to download big resolution images and downsize them and put them in the other folders.
Now how to know which device will take which image ? See the image below :-
See the drop down. It contains a list of devices with their resolutions and image type -hdpi ldpi mdpi xhdpi xxhdpi. Moreover, you can GOOGLE for the devices and their resolutions and dpi's.
So whatever app you are making for a device, choose that device and look at its resolution and see whether it is hdpi or ldpi etc. Accordingly, put the images and layouts.
This is my understanding. I create my layouts with this understanding. I hope you got a clear picture
For Multiple Screen Support
Add Different size of images to
drawable-ldpi/
drawable-mdpi/
drawable-hdpi/
drawable-xhdpi/
drawable-xxhdpi/
Suppose you have Imageview in layout folder
<ImageView
android:layout_width="#dimen/horizontal_len"
android:layout_height="#dimen/vertical_len" />
Do the following values
values/dimens.xml (Normal mobile)
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="horizontal_len">16dp</dimen>
<dimen name="vertical_len">16dp</dimen>
values-large/dimens.xml (7 inch)
<dimen name="horizontal_len">20dp</dimen>
<dimen name="vertical_len">20dp</dimen>
values-xlarge/dimens. xml (10 inch)
<dimen name="horizontal_len">22dp</dimen>
<dimen name="vertical_len">22dp</dimen>
So you have to try like this.

Categories

Resources