Android correct smallest width qualifier - android

This is my first venture in sw qualifiers and I appear to be missing the mark. Looking into this I see soo many directions on figuring out how to correctly calculate what is needed and through trial and error, I have landed back on error.
What I would like to do is define a layout (port and land) for all devices up to a certain size. Above that size use a seperate layout (port and land).
The 2 devices in my possession which identify the line in the sand so to speak:
4.7” 720x1280 (quantized density 300)
4.3" WVGA 480 x 800 (quantized density 240)
What I want to do is use a certain layout < 720x1280

In my experience, there are some consistent "buckets" used for smallest-width values. These are
default: used for everything
sw360dp: excludes the smallest phones (sometimes 320 or even 240 dp wide)
sw411dp: differentiates wider-screen flagship phones from narrower-screen ones
sw533dp: slightly undersized tablets (think the original Kindle Fire)
sw600dp: standard seven-inch tablets
sw720dp: standard ten-inch tablets
There's no hard line. It's quite possible for one phone to have a smallest width of 399dp and another to have 401dp; you just have to make a judgement call.
I'd recommend thinking about it not from the perspective of "how can I differentiate these two devices?" but instead from the perspective of "at what point do I have enough room to change my layout?" Maybe once you have at least 400dp to work with, you can change your layout to include more features. Or maybe you need 517dp to get that extra content in. When you think about it this way, then whatever bucket any given device happens to fall inside, it will have a layout that looks like it was designed for that screen size.
There are also some online resources that provide specs for a large number of devices; perhaps you can use these to determine where you want to draw the line. For example: https://material.io/tools/devices/

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

Are there any truly display independent units for placing things on android canvas/screen?

I'm developing a watch face for wear os. I was trying to figure out why the layout is messed up on different screen sizes and resolutions - I was using "dp" for all dimensions so I thought it will be "density independent", yes?
I made few emulators with different dpi and sizes and could not figure out why bigger change id dpi makes my layout look good again, but smaller changes introduce offset. I found out that:
One note about dp/sp that isn't totally obvious: The scaling that occurs for these depends not on the devices real density (dpi) but on which "bucket" of densities it falls into: available buckets are: 120,160,240,320. (...)
Note that pt should give you equivalent results as dp
So are there any truly display independent(proportional to screen) units I can use to position my elements?

Supporting Multiple Screens in Android

I have developed an android app and its working fine in my mobile of resolution 240*320.But if I install in another mobile some like 240*400,480*800, etc all the view components are changing according to the screen resolution.
I also created a sub folders under the res like layout-small,layout-large,layout-xlarge this procedure is also working according to the screen resolution the xml files are been read by particular folder.But this is not working on a 240*400,240*432,etc.., these type of screen resolutions are not supporting by the above procedure
What should i do for achieving all the view components to be look same for all screen sizes.
Thank you
This stuff is hard. You may have read the documentation, but you haven't understood it.
Basically, you have to distinguish two important things.
Layouts (XML) are determined by the dp size of the device. Forget about 'resolution' and just think about device independent pixels (dp). The dp size of a device is roughly equal to its size in inches, if it had 160 dots per inch. So a 10" tablet is about 8"x160=1280 dp long, while a 4" phone is about 3"x160=480 dp long. Approximately.
Drawables (PNG and JPG) are determined by the pixel resolution of the device. To produce the identical icon on a device that is 320 dpi and for one that is 160 dpi you need twice as many pixels. A 64x64 icon on the first is only 32x32 on the second.
So you need to produce a range of XML files (layout or dimen) that scale according to the desired dp size and put them in folders with names like sw600dp (shortest width 600 dp) and sw320dp. (Search SO or the web for sw600dp and you'll find lots to read).
And you need to produce a range of PNG or JPG files that scale according to the pixel resolution and put them in folders with names like mdpi and hdpi (search for that too).
Simple enough, but hard to do well in practice. We can only hope that eventually Android will fix this mess but for now this is what we have.
First off, stop using the term resolution. That term is ambiguous.
In Android, one could easily have a phone with a small screen with very high pixel density and a large tablet screen with very low pixel density, and the so-called resulting resolution on both devices could still end up being equal.
i have read that many times and implemented according to that but read
my question properly. – user3851899 3 hours ago
I'm sorry, but believe us, we've parsed your question very thoroughly.
http://developer.android.com/guide/practices/screens_support.html
You've mentioned the term "resolution" five times and yet in the document you've read many times, below is the only part where resolution is even mentioned, and even then, it's to tell us that you should "not directly work with resolution".
Resolution
The total number of physical pixels on a screen. When adding support for multiple screens, applications do not work
directly with resolution; applications should be concerned only with
screen size and density, as specified by the generalized size and
density groups.
Furthermore, can you count the term density is mentioned in that document. It's mentioned 171 times! The fact is, you've missed the main key take away concept from that document.
The term resolution is not very useful for Android development. The concept of size is important for a large background image taking the entire width or the entire height of the screen and it's important for layout issues that take into account the entire height of the screen or the entire width of the screen, but it's not very important otherwise. And what's really important for developing on multiple screens (aside from the scaling font size) is really the density of the screen.
So I implore you, please read that document again. Hopefully, you'll begin to understand it, now that I've reset some of your assumptions.

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.

Different Text Size under same screen resolution but different dpi in two Android machines

I have two Android machines - Samsung Galaxy Tab and HTC Flyer. They both have 1024x600 screen. However Tab has 240dpi screen while Flyer has 160dpi according to the log from Context.getResources().getDisplayMetrics() method.
My problem is that, I have a TextView defined, and according to those Android document, using unit of dp should give same physical size under different screen density(dpi). But what I observed is that, the text size appeared in Flyer is obviously smaller than that in Tab, no luck in using sp too. I want them to be in same size. Any clue in solving this problem in general?
Thanks in advance.
Well, the reported values are clearly a lie. I suppose the Galaxy Tab is the 7in model. This means that, based on the reported values, it has about 4.2in display height (1024 / 240 = 4.2in) and about 2.5in width. This means that the screen size would be 4.2x2.5 in - and in this case the diagonal would be about 4.9in. Is it correct - no it isn't, it has 7in display. Measure the sides of the screen and you would get the correct density. So, even when scaled by Android the result will be wrong because of the false density measurement.
Anyway, the answer of the question is:
No, no way to draw same-sized fonts, images and whatever. You need to get used to false metrics from the manufacturer. A false density reading reflects on all other scaling. Thus even mm/in would not help.
And one advise - don't try to match sizes between different products, unless absolutely necessary. If you succeed, then the text on the Galaxy Tab, for example, will look disproportional in regards to the other text on the device and will make your app look out-of-place. Stick to the Android textAppearanceSmall/Large, etc.
Please try the following units:
mm
Millimeters - based on the physical size of the screen.
in
Inches - based on the physical size of the screen.
http://developer.android.com/guide/topics/resources/more-resources.html

Categories

Resources