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
Related
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.
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 have an issue regarding usage of "dp".
In which context dp can be used? Is it just used for any controls i.e ImageView and TextView, or it can be used for adjusting layouts as well.
For adjusting layouts I'am currently specifying width and height of the controls in terms of percentage of the screens width and height.
it can used as unit to specify width and height.. you can use it with all views...see this for more..
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",
The use of dip or dp is to make your Android applications compatible with multiple screen densities and resolutions.
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
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.