I have seen some people using sp for margins like:
android:layout_marginLeft="40sp"
While many have been using dp, like:
android:layout_marginLeft="40dp"
Could anyone please confirm which is better between these two and which should be used when? Any help would be highly appreciated.
sp for font sizes, dp for everything else. sp stands for Scale-independent Pixels, dp stands for dip=density independent pixels. Detailed explanation
An XML-defined dimension value. A dimension is denoted by a number followed by a unit of measurement. For instance, 25px, 5in, 10dp and 10sp. When you use sp/dp, your Android applications will be compatible with a wide range of screen densities and resolutions.
PX: is an abbreviation for Pixels, which specifies the actual pixels on the screen.
SP: is an abbreviation for Scale independent pixels. It is the same as the dp unit, but it is additionally scaled according to the user’s font size selection.
DP: A virtual pixel unit used to communicate layout dimensions or location in a density-independent manner while creating UI layout. The density-independent pixel corresponds to one physical pixel on a 160 dpi screen, which is the system’s baseline density for a “medium” density screen. At runtime, the system handles any scaling of the dp units that is required based on the actual density of the screen in use in a transparent manner.
The terms DP and DIP refer to Density Independent Pixels, which are based on the physical density of the screen.
SP: Similar to dp, but also scaled by the user’s font size selection. When choosing font sizes, it is recommended that you use this unit so that they are adjusted for both screen density and user choice.
Related
I wand to handle the multiple screen handling in my app.
I see that in tutorials margin and padding are set in the dp.
I want to know if dp is the best unit for padding and margin? What other units are available for setting padding and margin?
In my opinion, using dp is the best choice for margin and padding.
The units available are:
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.
mm Millimeters - based on the physical size of the screen.
dp stands for density independent pixels. It works best on wider ranges of screens and different densities out there. It automatically scales based on the device its been on
TextView compute text size value with scaledDensity instead of density. so what`s the difference between these two values?
DisplayMetrics#scaledDensity
A scaling factor for fonts displayed on the display. This is the same as density, except that it may be adjusted in smaller increments at runtime based on a user preference for the font size.
DisplayMetrics#Density
The logical density of the display. This is a scaling factor for the Density Independent Pixel unit, where one DIP is one pixel on an approximately 160 dpi screen.
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.
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.
you can find nice explanation here Difference of px, dp, dip and sp in Android and Android units
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
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.
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.