values-sw resource issue in Android - android

I have two devices:
Moto-G first generation (720*1280, 4.5" 324.2 PPI)
ICE Tablet (600*1024 7" 169.55 PPI)
And created folders "values-sw420dp-xhdpi" and "values-xhdpi" for Moto-G and folder "values-sw360dp-mdpi" for ICE Tablet. But both devices taking the values from "values-sw360dp-mdpi" folder.
I am very confuse about this behavior of the devices because both are quite different, however why this is happening ?
Can anybody please guide me how to define sw folder for these two devices and in general how sw is working? I read the official guide line of sw folder and supporting multiple screens, but did not got exact idea.

According to your question, let me cite a fragment from Android Developers Guide:
Screen configuration: smallestWidth
Qualifier values: swdp
Examples: sw600dp sw720dp
The fundamental size of a screen, as indicated by the shortest
dimension of the available screen area. Specifically, the device's
smallestWidth is the shortest of the screen's available height and
width (you may also think of it as the "smallest possible width" for
the screen). You can use this qualifier to ensure that, regardless of
the screen's current orientation, your application's has at least <N>
dps of width available for its UI.
For example, if your layout requires that its smallest dimension of
screen area be at least 600 dp at all times, then you can use this
qualifier to create the layout resources, res/layout-sw600dp/. The
system will use these resources only when the smallest dimension of
available screen is at least 600dp, regardless of whether the 600dp
side is the user-perceived height or width. The smallestWidth is a
fixed screen size characteristic of the device; the device's
smallestWidth does not change when the screen's orientation
changes.
Full Text: Supporting Multiple Screens
Like #tiny said:
sw420dp-xhdpi means screenWidth = 840(420 *2) ,
sw360dp-mdpi means screenWidth = 360(360*1)
EDIT: Using this site http://pixeldensitycalculator.com/ I've already calcualated that
Moto G 1st edition has density 293.72 ~ 320dpi
ICE Tablet has density 169.55 ~ 160dpi
None of these devices doesn't have more than 320dpi, that's why none of them using your 420dpi folder.
Hope it help

Related

Android layout qualifiers

I currently work on an application which has to support different screen sizes. I read through the documentation about this and decided to create different layout with the smallest width qualifier. The documentations notes:
smallestWidth - sw<N>dp The smallestWidth is a fixed screen size characteristic of the device; the device's smallestWidth does not change when the screen's orientation changes.
Available width - w<N>dp This configuration value will change when the orientation changes between landscape and portrait to match the current actual width.
I did so and everything works great but nevertheless I am a bit confused: Due to device metrics the Google Pixel should have a minimum width of 411 dp. In my application I created a layout with the sw480dp qualifier but when I select the Pixel device from the Android Studio layout editor it always opens the normal layout without qualifier.
I also tried to create my own hardware profile for the Galaxy S6 (360 dp) through the AVD manager but it faces the same problem. Have I missunderstood something here?
I also do not quite understand this image from the documentation. How do I have to read it? The small, medium, etc. qualifier are deprecated so why are they in there or is this only used for conversions?
In my application I created a layout with the sw480dp qualifier but when I select the Pixel device from the Android Studio layout editor it always opens the normal layout without qualifier.
411 is less than 480. Hence, -sw480dp is not a valid qualifier.
Have I missunderstood something here?
The NNN value in -swNNNdp is the lower bound, not the upper bound. Devices with a smallest width of NNNdp or larger would use -sw480dp.
How do I have to read it?
I have no idea what information that image is supposed to be conveying.

How I should name the layout for 1920x1080 pixels and 5,5 inches device?

I want to create a special layout for a device with 1920x1080 pixel and with 5,5 inches.
Does anyone know how I have to name my layout for this?
Now I have for example for the Nexus 10 a layout named layout-sw720dp
I found anytime a calculator for this, but I cant find it anymore.
thx.
So first you need to understand what's the meaning behind layout-sw720dp
Based on Android Documentation on Supporting Multiple Screen sw mean smallestWidth and the format is layout-swdp.
so 720 the fundamental size of a screen, as indicated by the shortest dimension of the available screen area. Specifically, the device's smallestWidth is the shortest of the screen's available height and width (you may also think of it as the "smallest possible width" for the screen). You can use this qualifier to ensure that, regardless of the screen's current orientation, your application's has at least dps of width available for its UI.
For example, if your layout requires that its smallest dimension of screen area be at least 720 dp at all times, then you can use this qualifer to create the layout resources, res/layout-sw720dp/. The system will use these resources only when the smallest dimension of available screen is at least 720dp, regardless of whether the 720dp side is the user-perceived height or width. The smallestWidth is a fixed screen size characteristic of the device; the device's smallestWidth does not change when the screen's orientation changes

Creating a new sw<xxx>dp qualifier

I have the qualifiers for 7 inch tablets and 10 inch tablets, sw600dp and sw720dp respectively. I don't understand how those numbers were reached.
I would like to create a new layout for phones which a smaller screen size than 4 inches. Please could you explain how I should do this using the smallest width qualifier.
Please checkout "Configuration example" section of official documentation first. It explains very well what is used and when. In your case default layout folder without a qualifier is used for handsets.
What is smallest width qualifier? Smallest width is
The fundamental size of a screen, as indicated by the shortest
dimension of the available screen area. Specifically, the device's
smallestWidth is the shortest of the screen's available height and
width (you may also think of it as the "smallest possible width" for
the screen).
For instance your have a phone with screen size equals to 480x800 dp. The smallest width for this device will be the smallest value of these two, which is 480dp. If you rotate your device, the smallest value will stay unchanged - 480dp.
How to use smallest width qualifier? When you create a layout you always expect a minimum width, with witch it swill looks good. Below this minimum your layout gets squeezed and doesn't look good. To make sure this doesn't happen to it, you put it to the folder with sw<N>dp qualifier, when N the minimal allowed width.
It's worth to mention that because smallest width doesn't depend on orientation, you should take case for handling landscape and portrait orientation by yourself by using land or port qualifiers. Although this approach works, it can become complicated to handle different widths and orientations very soon. To address this issue, there is another approach called responsive mobile design. I suggest to read a series of articles "Deep dive into responsive mobile design" to get better understanding.
Update:
The formula for calculating dp from px is like following:
dp = px / (ppi / 160dp)
Nexus 7 takes sw-600dp, because all calculations are based on getResources().getDisplayMetrics().density value coded in the device. This is not the real value, but a rounded value. For Nexus 7 (2013) the real value is 323/160 = 2.01875. The value coded in the device is 2, which corresponds sw-600dp. This is where some pixels get lost.
sw600dp means smallest screen width of density 600dp. But let me tell you that you need more than this because Nexus 7 (2012) and Nexus 7 (2013) are both 7inch tablets but with different densities. You can add hdpi or xhdpi with sw600dp like values-sw600dp-hdpi. For phones do the same swXXXdp but try not to do that and use screen-size like small, normal, large, and xlarge.
For reference see Android official link

Is it correct way to create separate layout file for supporting different screens? [duplicate]

I'm really feeling confused. From the docs at developer.android.com, it seems in order to keep my images scaled correctly (aspect ratio too) across all current Android devices I need all these layouts below. Is that really what everyone is doing? Am I missing something, or should I be going about this a different way?
Low density Small screens QVGA 240x320
------------------------------------------------
layout-small-ldpi
layout-small-land-ldpi
Low density Normal screens WVGA400 240x400 (x432)
------------------------------------------------
layout-ldpi
layout-land-ldpi
Medium density Normal screens HVGA 320x480
------------------------------------------------
layout-mdpi
layout-land-mdpi
Medium density Large screens HVGA 320x480
------------------------------------------------
layout-large-mdpi
layout-large-land-mdpi
High density Normal screens WVGA800 480x800 (x854)
------------------------------------------------
layout-hdpi
layout-land-hdpi
Xoom (medium density large but 1280x800 res)
------------------------------------------------
layout-xlarge
layout-xlarge-land
Your app will work on 100% of the devices with the classic layout.
You can just add some buttons or change the layout in landscape mode by adding some qualifiers but that's up to you!
For instance, on LDPI (small resolution) device, you may want to adjust some buttons or change a little bit to fit the small screen.
You may also want to put some buttons on the right in landscape mode and in the bottom of your layout in portrait!
You do not "have to" use them.
According to Android Dev Protip from Roman Nurik about screen size qualifiers:
If you have custom layouts for larger screen devices such as tablets,
now's the time to stop using the -large or -xlarge resource qualifier
and switch to using -swXXdp or -wXXdp qualifiers. The latter were
introduced in API level 13, which basically all tablets now have
support for according to the latest platform version charts1.
means that for basically all cases where -large would have any effect,
-swXXdp can be used instead to provide more granularity.
So which actual sw or w qualifiers should you use? Here's a quick
just-give-me-something starting point:
7" tablets: Instead of layout-large, use layout-sw600dp.
Example: Nexus 7 = 960×600 dp; the smaller of the two dimensions is
600.
10" tablets: Instead of layout-xlarge, use layout-sw720dp.
Example: Nexus 10 = 1280×800 dp; the smaller of the two dimensions is
800. Some 10" tablets are a bit more narrow so 720 is a commonly used switching point.
There's more to it than just that (you really want to choose switching
points based on your content's minimum requirements, not on device
form factor), but that's a #Protip for another day (::cough:: +Nick
Butcher ::cough::).
In the layout if you do not use AbsoluteLayout, you application is going to be resized to fit the screen.
But in some cases, for smaller screens you need declare a new layout with less components for example.
For images:
You has some options.
Simple ignore some resolutions and let the device choose the best image to it.
Declare on AndroidManifest for wich sizes you want to support.
Or has just one and let the resize screw your application.
Yes. You should define different layout for different screen resolution.
When you are supposed to work with tablet kind of application. That time really you feel that. There is a need of different layout. Because Tablets always comes in different size.
One more thing, When you are working on android, means you should always taste your app with real device. And also test with different size emulators.

Screen resolution / Physical size

I'm implementing an app for Android(API 10). I have few designs for tablet(supposedly) and for mobile phone. Regarding the previous sentence it might sound a bit stupid: in my opinion layouts should be picked according to device's physical size - not resolution, otherwise there might be a phone with high resolution that renders, say, a grid of 5x5, in rather confusing, inadequately small-sized way. In order to provide division by physical size I gotta use /res/layout-* folders. Agreed ?
Also: I'm a bit confused about multiple-screens guide's definition on physical size. Would you mind explaining what physical size is in terms of Android mean, what it's measured in? Is there any correlation with dpi?
Thanks.
A workaround for api level 10 could be something like this:
use the compat lib from the SDK so that you can design with fragments.
Assume everything before android 3 is a phone. For example use your layout files in layout/* for this (and the rest will be based it on unless overriden). This assumption is basically only wrong for the original Samsung Galaxy Tab 7" from 2009.
Assume everything on android 3.x is a tablet (they are), so do some tablet specific layout if you want and have them under layout-v11 (and maybe also layout-v12 if there is anything specific to android 3.1). Tablet specific layout could mean that you arrange your fragments differently and/or show multiple fragments at the same time.
Everything newer (android 3.2+) you can use the new stuff from api level 13. Such as layout-sw600dp/ for some layouts etc etc. http://developer.android.com/guide/practices/screens_support.html#DeclaringTabletLayouts
Physical size is usually measured in inches, it simply tells you the real device's screen size (usually 3-4" inches for phones and 8-11" for tablets).
dpi, is NOT correlated with it. Dpi expresses screen density, how many pixels are shown in a given area (usually a square inch). It could be considered as a measure of screen quality.
Resolution is given by the product of the two; it expresses the total number of physical pixels on a screen.
Going back to your first question, you should be density independent as much as possible; your app should "look the same" on devices with different densities. The /res/layout-* folders are designed to provide this feature, the system scales drawable resources to the appropriate size and you could then declare in your manifest that your app supports any density:
android:anyDensity="true"
On page http://developer.android.com/guide/topics/resources/providing-resources.html#BestMatch you can learn how Android choose the best matching resource.
With Android API 10 the best option for targeting tablets is probably just distinguishing the actual size of the screen in the code using something like:
https://stackoverflow.com/a/5789916/1319155
and then just load a different drawable if the size value returned was greater than 6 (or whatever size you want to declare as a "tablet").
The reason you can't really just use the size folders (i.e. layout/large) is because the folders don't distinguish between phones and tablets very well. A kindle fire and galaxy nexus may both be considered "large" devices.
The reason you can't really use dpi is because that is not a good reflection on what type of device it is, just how "dense" the pixels are on a screen. Most new phones are much denser (having more pixels per inch) than tablets anyway.
There are two ways of doing this. From Android 1.6 (API 4) on, there are four layouts that describe the physical size of the display: small, normal, large, and xlarge. As described on http://developer.android.com/guide/practices/screens_support.html , these correspond to:
xlarge screens are at least 960dp x 720dp
large screens are at least 640dp x 480dp
normal screens are at least 470dp x 320dp
small screens are at least 426dp x 320dp
Note that these are measured in DP, not DPI. DPI is Dots Per Inch, and specifies screen density. DP, also written DIP, are Density-Independent Pixels. Again from the guide:
Density-independent pixel (dp)
A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way.
The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.
In other words, 160 DP = 1". Applying this standard, we see:
xlarge screens are at least 6" x 4.5" (7.5" diagonal)
large screens are at least 4" x 3" (5" diagonal)
normal screens are at least 2.9" x 2" (3.5" diagonal)
small screens are at least 2.6" x 2" (3.3" diagonal)
(Not sure why the selection is so odd, but that's what they defined.)
From Android 3.2 on (API 13), there are more options, as described here:
http://developer.android.com/guide/practices/screens_support.html#DeclaringTabletLayouts
Here you can use "smallest screen width," "available width," or "available height" options to define your own categories; again, the unit in question is DP, which is 1/160". Note that these specify the smaller of the two dimensions on the device--e.g., for a 7" tablet they recommend specifying layout-sw600dp, that is, "smallest width 3.75 inches," which would be intermediate between the "large" and "xlarge" sizes defined in API 4. They have a number of specific comments about this topic, including notes about how the widths are measured (it may exclude things like the notification bar), so it's worth taking a look at the documentation.

Categories

Resources