Creating a new sw<xxx>dp qualifier - android

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

Related

How to test android app on all screen sizes

I've created my app but I decided to test it on all screens to be adaptive. Maybe testing it in
MDPI(160dpi), HDPI(240dpi), XHDPI(320dpi), XXHDPI(480dpi) and XXXHDPI(640dpi) which corresponds to all screen sizes. However I tested my app on two devices support 320dpi. I thought that I get the same result but it don't. so don't know how to test my app on all screens. I'm confused.
I used Genymotion for emulators:
one with 720 * 1280 320dpi
screenshot
and other with 1200 * 1920 320dpiscreenshot
Please help me with that or if there's another way to do it let me know.
thanks in advance
(Bunch of explanation about why this is happening, I put my suggestions in the last section)
DPI isn't the same as size, it's just about how many pixels are packed into a certain area. The higher the DPI, the more pixels there are, meaning they're a lot smaller. So you can get fine detail, but it also means you need more of them to cover a physical distance or area on the screen.
Which is why Android uses dp instead of raw pixel sizes most of the time - the standard minimum touch-target size is 48dp, but how many actual pixels that is depends on the pixel density of the display. For mdpi displays it'll actually be 48 pixels, for xxxhdpi it'll be 4x that amount
It also means if you're doing your design work in dp, the pixel density of the screen doesn't matter - elements with a fixed size will always be broadly the same size on every screen (the mdpi etc buckets are like a "close enough" grouping, the devices in each group won't all have exactly the same DPI) because it's getting translated to the equivalent number of actual pixels. What testing does help with is checking your drawable assets look ok on different screen densities
So you have two devices with the same DPI right?
720 x 1280
1200 x 1920
Because they're the same DPI, those dimensions are converted to dp by the same factor. Let's work it out to be precise (but the exact numbers aren't important): 320 DPI is xhdpi according to that link up there, so 1dp = 2px. Let's convert those screen sizes to dp
360dp x 640dp
600dp x 960dp
It's the same situation but hopefully having those sizes expressed in dp helps you see the problem - when you're designing a layout, you're working with dp, right? The available space you have to work with is defined in dp, and one of those has a whole lot more space than the other! Putting a 300 dp-wide TextView in the layout would almost fill the first one horizontally, but it would only cover half of the second. That's gonna look pretty different!
This is why phones and tablets look so different - even if you have a relatively new phone, and an old Nexus 7 with a much lower resolution, the Nexus 7's screen is going to feel "bigger" and more "spacious". It's a physically larger device, so even though it's low-res, it's also low-density which means those pixels are spread across a larger area. Lower DPI means those pixels translate into more inches. And in the density-independent pixel (dp) system, that means you get way more dp to work with, more space for your layout. Which is what you want on a tablet, you don't want it to look and feel like a massive phone!
That's why it's different - basically one of your devices has more space to work with than the other, because it has more pixels, and the same DPI so those pixels aren't just used for finer detail.
As for testing, you need to look at different screen sizes - which is dependent on the resolution and the DPI. Basically pixels / DPI = size in inches. Do that for your two 320 DPI examples and you'll see one is a fair bit larger, physically, while having the same density
Probably the easiest way to do this, really, is to look at your layout in the design view, and change the Preview Device setting at the top. Some of the Phone devices are more "spacious" than others, newer ones are taller, so go through them and see how your layout changes. Try a Tablet one and see what a lot of extra space does. And if you go down to the Generic Phones and Tablets section at the bottom, there are a bunch of reference devices in there, some of which will be very cramped!
Once you've found a few useful ones, you can set up your emulator / virtual devices with each of their screen resolution / density combinations. I don't know about Genymotion, but the built-in AVD manager gives you a lot of those device definitions as templates when you create a new virtual device. You should at least be able to enter those settings yourself
Also when you publish an app on the Play Store, they'll automatically run it on a bunch of reference devices and give you access to a bunch of screenshots, so you can see if there are any problems with certain screen sizes and fix them before you launch

values-sw resource issue in 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

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

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.

Android screen size compatibility

I have a two questions.
First: I am looking at the Android compatibility definition document (CDD 4.0) and it states:
Devices MUST have screen sizes of at least 2.5 inches in physical diagonal size
Devices must report one of these densities: 120dpi, 160, 213, 240, 320
The aspect ratio must be between 1.3333 and 1.85
Must have minimum screen size of 460dp x 320dp (dp = density-independent pixel)
Suppose I have screen of 2"x3", with density of 120dpi, the screen would have:
Diagonal: 3.61" = good
Screen size: 320x480 dp = good
This is nicely compatible with Android CDD
If I change the width from 2" to 1.7", I get
Diagonal: 3.45" - still good
Screen size: 272x480dp - NOT COMPATIBLE
My first question is, why specify a diagonal value, when the WIDTH is really what affects compatibility? The width must be 2" minimum.
Second: If I don't need to be Android compatible and stick with the 1.7" screen size, will applications that were built for the smallest compatible display be able to run on my device? Will the UI of this application be cropped when run in my device?
Thanks much for any insight.
It's probably just an easy way to specify the requirement. If you have a portrait device, the width is smaller than the height. If you have a landscape device, the width is the large dimension. It's just easier to say "the diagonal" than "the smaller of the two screen dimensions when the device is laid flat" or something like that. There are lots of ways to say essentially the same thing.
If your device is smaller than the CDD requires, you will probably want to still report in Android that your device is of screen size small, so I imagine apps will run but layouts for many of them will likely not fit well as people will tend to test on CDD-conformant devices. It very much depends on what layouts people used, but I wouldn't be surprised if you saw some cropping.

Categories

Resources