Different dimens.xml for different screens - android

I have to provide two different font sizes for two different types of devices. The first group are smaller devices (e.g. the Nexus S, or devices with around 480px height) and the second group are all other phones (e.g. Galaxy Nexus, Nexus 4, 5 and so on). I already supported tablets.
By now I created a values-sw320dp folder with a dimens.xml in it - for smaller devices to use, and there is also the default values folder with their dimens.xml in it, to support all other phones.
The problem is that the dimens.xml from the sw320dp folder is applied everywhere.
Am I supposed to create a values-sw340dp or values-sw360dp folder for those devices? What is the purpose of the dimens.xml in the default values folder if I am going to bucket devices by their smallest width? How does a device put itself in a density bucket, look for their closest sw-dp folder or what?
I know that this question has been asked a thousand times before, but I really could not google it out or find it on SO.

The Nexus S is a common hdpi device (240 dpi), with a resolution of 480x800 pixels.
Therefore, you can use the values-hdpi for storing that device's dimens .xml.
You might consider to support smaller devices such as mdpi or ldpi ones, as well.
And "not so smaller" devices too (i.e.: xhdpi and xxhdpi).

There are different types of qualifiers (IMHO), ones are responsible for density and the others are more about screen sizes, and there are some identifiers for tablets. It is not only having a single suffix like hdpi for folder, you can play with couple of them.
You can find more info on the Official website:
http://developer.android.com/guide/practices/screens_support.html

Related

Best Practice: res folders - Xdpi vs swXXXdp

I've been developing for about 5 years and have always used 'dpi' folders in my res directory (mdpi, hdpi, xhdpi, etc) with 'sw' folders for tablets (sw600dp, sw720dp, etc).
I've noticed in Android Studio that, starting with the Nexus 6, google lists its newer devices in sw buckets rather than in dpi buckets.
I know we can still use dpi folders but SHOULD we? Or should we be transitioning to sw folders for all device sizes?
Or does it not matter?
google lists its newer devices in sw buckets rather than in dpi buckets.
First, those are independent concepts:
sw is "smallest width" and refers to size
dpi is "dots per inch" and refers to screen density
Also, there is no occurrence of "sw" in that screenshot that I can see. So, I do not know what is worrying you.
One guess is that you are looking at values like 420dpi and xhdpi and thinking that they are different concepts. They are both densities. The Android Studio UI is listing finer-grained density buckets than we use for our resources. xhdpi is 320dpi, for example.
Another guess is that you are looking at values like 420dpi and thinking that means -sw420dp, which is not the case.
I know we can still use dpi folders but SHOULD we?
For bitmaps, yes. For anything else, no.
Or should we be transitioning to sw folders for all device sizes?
Using size-based folders — -wNNNdp, -hNNNdp, or -swNNNdp for varying values of NNN — may be necessary depending on your UI. If you need different layouts or margins or padding or something based on screen size, those folders are useful.

How to handle images in low density large screens in Android?

I'm writing an Android app and have read up everything I can find on fragmentation and handling multiple screens, particularly from the official Android documentation.
I understand different screen densities utilize the various folders (mdpi, hdpi etc.) for images of varying sizes, and different screen sizes can utilize different layouts by defining them in res/layout-sw600dp layout-sw720dp etc. which all makes sense.
However, when developing my app my primary test device was a HTC Desire X phone 400x800 with 240dpi density and all looks well. I then tested it on a Prestigio Multipad 8.0 tablet and this is where I'm getting confused. The tablet has a screen size of 786x1232 BUT the screen density is only 160dpi. Owing to the fact that the screen density on the tablet is smaller, Android therefore uses the images in the mdpi folder for the tablet, and the slightly bigger ones in the hdpi folder for the phone! This therefore results in images way too small on the tablet.
I don't need to edit the layout for the tablet so the sw720dpi folders aren't a help.
What am I missing or not understanding properly here? How can I make my images scale up for the bigger, lower density tablet screen?
OK, I figured this out eventually. My mistake was I didn't realize drawables work exactly the same as layouts in that I could have a drawable-sw720dp folder which Android fetches the images from in the case of a screen with at least 720pixels of width. For completeness in the scenario I described I could have a drawable-sw720dp-mdpi folder in which to store my images.
I had an additional issue then whereby I had 2 copies of the same image in different drawable folders but I discovered you can reference images using an alias just like layouts. Details of how to do that can be found here.

Is it possible to target Specific Android phones using the /res/values folder

I'm developing an app that targets only Android Phones, not tablets. The app should work in portrait mode only. I have all the dimensions (for views and fonts) in a dimens.xml file in the values folder.
I want to adjust the appearance for some commonly used phones, for example
Galaxy S2: 240dpi, 480x800
Nexus 4: 320 dpi, 768x1184
Nexus 5: 480 dpi, 1080x1800
I want to specify some font size and view dimensions specifically for these targets. I've tried with a lot of different values folder names, for example:
values-sw240dp
values-sw480dp
values-sw320dp, and so on,
but the app uses always the same folder for the three devices (normally the 480dp folder).
It is possible to specify dimensions in a resource file targeted for these devices?
Thank you in advance!
PS: excuse my bad English.
Yes it is possible. Every single folder inside the /res/ folder works exactly the same regarding those filters (xhdpi, sw600dp, en, land, etc).
The problem on your case is that you did not specify the correct folders. And the correct folders depends on what is the parameter you're using to specify the different dimensions.
sw___dp is for "smallest width", meaning, the smallest width of the screen (in a 800x480 phone is the 480 side) is that amount of DPs.
to specify DPIs you probably want to use hdpi, xhdpi, xxhdpi. Which will fit into those 3 devices you mention.
If you really want to use sw___dp, you probably want to install THIS APP on the devices and it shows you the sizing in DP for each screen (alternatively, if you don't have the devices, you'll have to do some math.
here is a comprehensive documentation of all the qualifiers you can use for resource folders.
It seems the best you can do is to use the dpi classes, and actually, I think that it's more correct cause you are optimizing you resources for every device in that class instead of only a specific few leaving the others to unknown results.
Not for specific devices, but for device sizes, it is possible using value folders named like these (width-height):
values-w480dp-h800dp

I am confused with drawable-hdpi and drawable-mdpi

i am attempting to get my android app to work on both phone type devices and tablets. for the layouts i have layout-normal and layout-large and i place my xml files inside each and it seems to select the correct one for each device (layout-normal for phones and layout-large for tablets).
however the problem arises for the drawable folders. i have drawable-hdpi, drawable-mdpi and drawable-ldpi. the tablet is taking its images from the drawable-mdpi, and the phone is taking its images from the drawable-hdpi. from the reading i have been doing this seems to be backwards??? this seems to happen on both the emulator and real devices, could i be doing something wrong? am i mixing something up?
Clayton,
drawable-hdpi and drawable-mdpi is not related to the size of the screen itself, but to the resolution of the display.
It's probably easy to think in terms of the iPhone 3GS and 4 for this (bear with me please). The 4 has retina display, hence a much higher resolution. Using the same sprites you'd use with the 3GS would result in a blurry display.
Android phones are, if anything, much more diverse in terms of screen options.
If you want to be able to aim different layouts at different resolutions, please try putting the layouts into /layout-xlarge, /layout-large, etc...
You can find more here:
http://developer.android.com/guide/practices/screens_support.html
There are some changes that you can see for Android 3.2 and above:
Beginning with Android 3.2 (API level 13), the above size groups are
deprecated and you should instead use the swdp configuration
qualifier to define the smallest available width required by your
layout resources. For example, if your multi-pane tablet layout
requires at least 600dp of screen width, you should place it in
layout-sw600dp/. Using the new techniques for declaring layout
resources is discussed further in the section about Declaring Tablet
Layouts for Android 3.2.
ldpi, mdpi, and hdpi refer to pixel density, not screen size.
The accepted answer does not address a serious problem: if you have many drawables, you end up with an unnecessarily big apk file because of resource duplicating. The only solution for that is the resource alias option described here:
http://blog.evendanan.net/2011/03/Android-resources-and-device-fragmentation
Edit: I just beheld that OP didn't realize that most phones have hdpi density, while most tablets have mdpi density. At the same time the tablets mostly have higher resolution. Hence Android will use smaller pictures for higher resolution.
You can overcome this only by using vector images or using the methods described in the accepted answer and in my answer.
drawable-hdpi - application use this drawable images when the device has high pixel density
drawable-mdpi - application use this drawable images when the device has medium pixel density
drawable-ldpi.-application use this drawable images when the device has low pixel density

Drawables and Layouts for Galaxy Tab and Motorola XOOM

I'm testing my app at Galaxy Tab and Motorola XOOM emulator skins, and I can't understand, why the drawables are taken from the wrong folders. I have drawable-xhdpi folder, where I store big size pictures. However, Tab takes drawables from drawable-hdpi, and XOOM uses the drawable-mdpi folder. So, how do I manage my resources? Thanks in advance.
In addition, it feels like view sizes defined in sp are not ported properly. I have some views which need to be 80sp x 80sp, on phones with mdpi and hdpi screens they look right, but on tablets they look too small. Why can this be?
However, Tab takes drawables from drawable-hdpi, and XOOM uses the drawable-mdpi folder. So, how do I manage my resources?
There is no single "Galaxy Tab" device, so I have no idea which one you are referring to there. The XOOM is an -mdpi device. I would expect that the Galaxy Tab 10.1 would be -mdpi as well. However, in the end, device manufacturers will choose values that they feel deliver the best results for existing applications.
GalaxyTab 7" use "-large-hdpi"; XOOM 10.1" use "-xlarge-mdpi".
check this post for details:
GalaxyTab, Motorola Xoom Screen Desity and Size
I have test with multiple screen resolutions and find out Android use the following qualifier for different screen resolutions:
240x320: -ldpi
320x480: -mdpi
480x800: -hdpi
GalaxyTab7.1 inches: -large
GalaxyTab10.1 inches: -xlarge
Hope this information is useful for you.
Galaxy tab (the old one, running Android 2.x) is indeed not xhdpi. I had success by using the "large" qualifier to identify tablets. Else you can always use an API qualifier such as v11 for Honeycomb only resources.
Rather than using the dpi qualifiers you can use screen size qualifiers instead, theres info here on exactly what tags you can use when selecting which resources to use,
http://developer.android.com/guide/topics/resources/providing-resources.html
It's important to understand the difference between dpi and screen size, dpi tends to be high on phones, like a samsung galaxy s2, but on the big 10 inch tablets it tends to be low(which makes sense if you think about it, the phones squeeze more pixels into a smaller space).

Categories

Resources