Android multiple screen support - android

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

Related

Should I really not use px over dp in 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.

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: 50dip is different size on different devices?

My understanding of the density independent pixel, is that it will render as the same size regardless of the screen dpi and dimensions. I created an image button of with and height of 50dip. On my medium dpi Samsung Galaxy Tab, this button measures about 10mm x 10mm (measured with a ruler. On my large, hdpi Acer Iconia Tab, the button measures about 7mm x 7mm. I would have expected the two buttons to have the same actual size. Am I doing something wrong, or is my understanding incorrect?
Both previous answers are correct. The problem lies in that not only changes the size in inces, but also the number of total pixels.
For a solution, you will need to create different xml layout files in res/layout-normal and res/layout-large, to adjust for screen resolution changes. Still, given that these folders group ranges of devices, you will have some variation in size.
Alternatively, if you strictly need a specific fixed size for a View, set up its width and height as "10mm"
public static final int layout_width Since: API Level 1
Specifies the basic width of the view. This is a required attribute for any view inside of a containing layout manager. Its value may be a dimension (such as "12dip") for a constant width or one of the special constants.
May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).
http://developer.android.com/reference/android/R.attr.html#layout_width
For a list of screen sizes, resolutions and dpi values, take a look at: http://en.wikipedia.org/wiki/List_of_displays_by_pixel_density
To calculate the real dpi value, check here: http://en.wikipedia.org/wiki/Pixel_density#Calculation_of_monitor_PPI
You've got it the wrong way round, the Galaxy Tab is HDPI and the Acer Tab is MDPI.
50dips is 50 pixels on an MDPI device or 75 pixels on an HDPI device, which should translate to roughly the same physical size.
However things are slightly different due to the much larger screen size on 3.0 tablets. In these cases often using the HDPI assets gives a better size.
from: http://developer.android.com/guide/practices/screens_support.html (emphasis added)
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.

When we need to go for size in terms of ps insted of px?

i am new to android..can any one tell when we need to go for size in terms of ps insted of px?..thanks in advance
measurement types:
px
Pixels - corresponds to actual pixels on the screen.
in
Inches - based on the physical size of the screen.
mm
Millimeters - based on the physical size of the screen.
pt
Points - 1/72 of an inch based on the physical size of the screen.
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".
sp
Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference.
source: http://developer.android.com/guide/topics/resources/more-resources.html#Dimension
fill_parent attribute is used expand to fill any remaining space in the parent view...
Generally, you should use density-independent units (like dp) instead of pixels. There's a detailed guide with examples here.
Firstly, do you mean 'pt' (or Points) instead of ps?
Px is short for pixels - use this if you want to be sure something is an absolute number of pixels in size. 'pt' is a publish measure meaning '1/72' of an inch. As this is a real-world measurement, it is not tied to pixels. Pixels can be of different sizes on different devices at different resolutions.
So, if you want to make sure something is of a certain real-world physical size, use 'pt' but if you want to make sure it is a set number of pixels, use px.

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.

Categories

Resources