Should I really not use px over dp in Android? - android

There are a lot of questions on this on here, but none that answer my specific question. When I use px, eclipse tells me to avoid it. So I did. But when I use dp, the grid/borders I am making around my cells in GridView seems too fat. I want it much thinner. When using 1 px as opposed to 1dp, I get the desired result. I understand warning on avoiding it, but should I really avoid it in this case?

Yes you should. "dp" will give pretty the same look on all devices, but pixels are not.
http://developer.android.com/guide/practices/screens_support.html
Long story short:
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.

px doesnt work on all devices i think it stopped working in android 2.2
I recommend sticking with dp.

Related

Why "dp-to-pixel" ratio changes with the screen density, but not necessarily in direct proportion

I am a bit worried by what I read here, which kind-of implies that there is no reliable formula to compute px based on dp and screen density (and vice versa):
dp :
Density-independent Pixels - An abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi (dots per inch) screen, on which 1dp is roughly equal to 1px. When running on a higher density screen, the number of pixels used to draw 1dp is scaled up by a factor appropriate for the screen's dpi. Likewise, when on a lower density screen, the number of pixels used for 1dp is scaled down. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Using dp units (instead of px units) is a simple solution to making the view dimensions in your layout resize properly for different screen densities. In other words, it provides consistency for the real-world sizes of your UI elements across different devices.
I thought we could always use the following formula (explained here) :
px = dp * (dpi / 160)
However, the testimony in this question seems to highlight a case where specifying sizes in dp does not guarantee a fixed perceived size across devices.
What is the real meaning of the sentence in bold ?
Do we need to use mm in order to be sure to keep the same perceived size ??
I thought we could always use the following formula (explained here) : px = dp * (dpi / 160)
This formula can always be used to convert between pixels and dp. Just make sure you use DisplayMetrics.densityDpi as the value for dpi in the formula.
However, the testimony in this question seems to highlight a case where specifying sizes in dp does not guarantee a fixed perceived size across devices.
Yes it is true that a certain dp does not guarantee a fixed perceived size across devices. It does however guarantee a similar perceived size across devices. The reason for this is that on some devices densityDpi (the density bucket) is different than the physical dpi of the device. Please see my answer to the question you linked for more details.
The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion
Here I think they mean the actual physical screen density when they say "screen density" and not which density bucket the phone belongs to.
For example a device with a physical dpi of 270 will belong to the "high" (240) density bucket and have a dp-to-pixel ratio of 240/160 = 1.5
Another device with a physical dpi of 290 will belong to the "xhigh" (320) density bucket and have a dp-to-pixel ratio of 320/160 = 2
When you compare these devices with each other the physical screen density has increased by 7% (270 -> 290) while the dp-to-pixel ratio has increased by 33% (1.5 -> 2) so it's not a direct proportion between them.
Do we need to use mm in order to be sure to keep the same perceived size ??
If it's important that the preceived size is as similar as possible between devices you have to use mm or inch. However this have some drawbacks as I mention in the question you linked.
It is also important to keep in mind that there are some devices where the mm and inch units are broken, for more information on this see the question Why Lint shows warning when using in (inch) or mm (millimeter) units as dimension?

Why use unpredictable dips in Android instead of predictable pts?

72 pts is always equal to 1 inch, yet 160 dip is not always equal to 1 inch.
As such, why do people recommend us to use dips instead of pts?
Isn't it better to base our dimensions on pts which is predictable, instead of dips which is unpredictable?
The reason why you should use dip (Density Indepentdent Pixel) is that this way your application will support multiple screen sizes. This means that if you set a value to 100dip, it would be translated into 75px on a low density screen (ldpi), 100px on a medium density screen (mdpi), 150px on a high density screen (hdpi) and 200px on a extra high density screen (xhdpi) but the layout will look the same on each screen (but scaled to the screen density).
The only reason to use pt is if you actually need the exact size and don't care about the screen density (I can't see when that would be the case).
You should read through this article to better understand why dp/dip is the way to go:
Supporting Multiple Screens
Also this similar question has a good explenation of the difference between the different units:
Difference of px, dp, dip and sp in android
Always use dip (Device Independent Pixel), this will respect the width and height of the device you're running on. So by using dip, the layout will be the same for all devices.
if you want your app to work in just a single device or a single resolution, you can use pts. if you would like your app to work on multiple resolutions (which i think you would) you are better going with dip or dp. Try your app on different resolutions using the emulator and you would see what the issues are. The app will look different in different resolutions.

Android multiple screen support

I am confused about android's multiple screen support. http://developer.android.com/guide/practices/screens_support.html I read this article, but still something is not clear for me.
I used dp instead of px in my layout
I put high, medium, low version's of an image to drawable directories.
I made this changes according to this article. But in some densities, there is still problem although some of them work very well.
The question is what is the exact width and height in dp units for variety of android screen types. if it is changeable, what is the difference between px?
px is changeable, dp is changeable too??? what is the difference??
if changeable, should I change the view's width and height by code on Create function or create seperate layouts for each screen dentisies? Please give a way to understand this...
Thanks in advance..
px are not changeable. dps or dips are.
To calculate how many px your object specified in dps will be use the formula below:
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.
px is a fixed measure. This means that if 100px on a small screen take up 1/2 of the screen, it will take up much less on a large screen. dp = density (independent) pixels, is based on the densitity of the device. So if you specify a width to 50dp on a small screen, it will expand on a large screen. Note that dp is not an insurance of layout compability on all devices, since devices have different aspect ratios. To build a perfect layout, that looks exactly the same on all devices, you must use more techniques. Linearlayout allows you to assign weights. Look into that. http://developerlife.com/tutorials/?p=312

basics of device-independent-pixels

im throughoutly confused by dips on Android.
I understand from the reference that the base for dp values is 160.
So, shouldn't 80dp in width equals a view with a width of 50% of the screen ?
On my Nexus One the width in dp is something around 300dp as it seems.
What am i missing here ?
thx in advance
"dp" == "Density-independent Pixels" (This is also why it was earlier called "dip", though I prefer to use "dp" these days.)
Think of it like other units -- "in" (inches), "mm" (millimeters), etc. It allows you to provide a size that is scaled based on the density of the screen.
We define mdpi to be the base density, so "10dp" on an mdpi screen will result in exactly 10 pixels. On an hdpi screen it will result in 15 pixels, because hdpi is 1.5*mdpi.
Note that though the constants for various densities are similar to DPI (mdpi is 160, etc), density is not exactly DPI. It is an abstract scaling factor that adjusts for screen dpi, but does not try to exactly reflect it. (You would use "in", "mm", etc for exact sizes but 99.9% that is not what you want so stick with "dp".) This greatly simplifies life for everyone because you don't need to deal with many Android devices having a slightly different amount of space for its UI because they each of slight different screen DPIs. Also, device manufacturers can select the density of their device to achieve a desired UI -- for example the Samsung Tab uses a density that is a fair amount larger than the actual DPI, resulting in an overall larger UI.
160 dots per inch. So 80dp would be 1/2 an inch, roughly.
I don't understand your question completely but I suggest you take a look at this, if you haven't already.
http://developer.android.com/guide/practices/screens_support.html
pixels = dps * (density / 160)
Density independent pixels (short: dp) are a virtual pixel unit that will be determined at the runtime of your application.
Formala:
1 dp = 1 Pixel on 160 dpi screen.
So 160 dpi is the baseline density for the system.
The conversion of dp units to screen pixels are quite simple.
Actual device pixels (px) = dp (1) * (dpi (of the device) / 160(baseline) )
For the sake of simplicity: px = dp * (dpi / 160)
Example:
If a 240 dpi device starts your app, then 1 dp equals to 1,5 actual device pixels.
Conclusion:
Dp automatically handles any scaling to bigger or smaller devices. The times where you hardcode the pixels are over. DP ensures the proper scaling on different screen densities.

Photoshop design from px to dp

I've got a final app design made i Photoshop where everything is measured in PX.
Now I realize that Android apps are using DP for font-sizes and other things.
Is there any way I can convert PX to DP ?
On a more practical note, you have a few choices in increasing work and fidelty:
Have your resources scaled for 160 dots per inch, put them in your res/drawable directory, and let the OS scale them to look right on the device.
Make two copies of your resources: one at 160 dots per inch in your res/drawable and one at 240 dots per inch in your res/drawable-hdpi directory. Let the OS scale for the exact device starting from a pretty close number.
Decide that you don't want any scaling and have raw pixels, so put your assets in the res/drawable-nodpi directory. This means that at 320x480 (pixel) graphic might be 2 inches by 3 inches on one phone but only 1 1/3 inches by 2 inches on another screen.
Specify the exact scaling strategy for your work, using the draw9patch tool. This can be very useful for keeping the corners of boxes from getting the "jaggies" from scaling and for making full screen graphics cope with different aspect ratios.
Make separate graphics for every device you care about and fall back on scalable graphics for the rest. You will need to outrun zealots waving style guides trying to convince you not to do it this way.
Oh, and as a gotcha, specify sp for your fonts, instead of dp or pt. A 10 point font would be a 22 sp font [ =10 * (160/72) or = number of points times 2.222]. sp units scale with user preferences for small, medium, or large fonts.
From this list of the dimension units supported by Android, here's a description of DP:
Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".
This means that the "conversion" between pixels and DP will not be consistent -- on some devices, the ratio might be 1DP = 160px, but it could theoretically be anything. This is all well and good when you're setting the width of a button to, say, 100dp (since it will get rendered dynamically), but it presents a problem when you have images which must have a fixed size.
Read this page on "Supporting Multiple Screens" -- Android has something called resource directory qualifiers, which let you create size- and density-specific versions of your image resources. For example, for low-density screens, you could create a smaller version of your image and place it in the drawable-ldpi directory (or drawable-hdpi for high-density screens).
tl;dr You can't practically "convert from PX to DP" (since the ratio is not fixed), but you can create multiple versions of your images and tell Android which to use with resource directory qualifiers.
A pixel is PX and the DP or DIP are device independent pixels. I don't think that you need to convert these. But you can use scalable 9patch images using the draw9patch tool from the android tools.
I have same issue but now got the Solution.
Why you not use the online convertor to see which dp or px you have to for different resolution of android device ??
See this link: this link which helps me a lot and also helps you.
Enjoy coding.
This is a online DP/PX Converter tools: http://labs.rampinteractive.co.uk/android_dp_px_calculator/

Categories

Resources