What is the difference between “px”, “dip”, “dp” and “sp”?
What is the difference between “px”, “dip”, “dp” and “sp”?
What is the difference between android units of measure?
This nifty converter demonstrates it best, in my opinion. It's also extremely useful for exporting sprites from Photoshop or designing your layout for a physical dimension.
– Paul Lammertsma
Jul 1 '14 at 9:28
new description on Google Design Units and measurements
– Arnav M.
Apr 27 '15 at 11:31
Difference between DP, SP And Pixels with examples.
– Ajit Singh
Mar 12 '16 at 12:15
How to programmatically convert between
px, dp, and sp– Suragch
Jul 15 '17 at 10:40
px
dp
sp
34 Answers
34
From the Android Developer Documentation:
px
Pixels - corresponds to actual pixels on the screen.
in
Inches - based on the physical size of the screen.
1 Inch = 2.54 centimeters
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 or dip
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 recommended you
use this unit when specifying font sizes, so they will be adjusted
for both the screen density and user's preference.
From Understanding Density Independence In Android:
+----------------+----------------+---------------+-------------------------------+
| Density Bucket | Screen Density | Physical Size | Pixel Size |
+----------------+----------------+---------------+-------------------------------+
| ldpi | 120 dpi | 0.5 x 0.5 in | 0.5 in * 120 dpi = 60x60 px |
+----------------+----------------+---------------+-------------------------------+
| mdpi | 160 dpi | 0.5 x 0.5 in | 0.5 in * 160 dpi = 80x80 px |
+----------------+----------------+---------------+-------------------------------+
| hdpi | 240 dpi | 0.5 x 0.5 in | 0.5 in * 240 dpi = 120x120 px |
+----------------+----------------+---------------+-------------------------------+
| xhdpi | 320 dpi | 0.5 x 0.5 in | 0.5 in * 320 dpi = 160x160 px |
+----------------+----------------+---------------+-------------------------------+
| xxhdpi | 480 dpi | 0.5 x 0.5 in | 0.5 in * 480 dpi = 240x240 px |
+----------------+----------------+---------------+-------------------------------+
| xxxhdpi | 640 dpi | 0.5 x 0.5 in | 0.5 in * 640 dpi = 320x320 px |
+----------------+----------------+---------------+-------------------------------+
+---------+-------------+---------------+-------------+--------------------+
| Unit | Description | Units Per | Density | Same Physical Size |
| | | Physical Inch | Independent | On Every Screen |
+---------+-------------+---------------+-------------+--------------------+
| px | Pixels | Varies | No | No |
+---------+-------------+---------------+-------------+--------------------+
| in | Inches | 1 | Yes | Yes |
+---------+-------------+---------------+-------------+--------------------+
| mm | Millimeters | 25.4 | Yes | Yes |
+---------+-------------+---------------+-------------+--------------------+
| pt | Points | 72 | Yes | Yes |
+---------+-------------+---------------+-------------+--------------------+
| dp | Density | ~160 | Yes | No |
| | Independent | | | |
| | Pixels | | | |
+---------+-------------+---------------+-------------+--------------------+
| sp | Scale | ~160 | Yes | No |
| | Independent | | | |
| | Pixels | | | |
+---------+-------------+---------------+-------------+--------------------+
More info can be also be found in the Google Design Documentation.
To calculate dimensions on real device this app can be used.
even though they should be the same thing I tend to have lots of problems with dp while everything works fine with dip
– DallaRosa
Jul 4 '11 at 6:07
One note about db/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. This can cause some problems handling screens that are significantly different but get bucketed the same.
– Fraggle
Oct 29 '11 at 4:10
Note that the documentation no longer mentions "dip" at all, just "dp", although the compiler still seems to accept "dip".
– Adam Rosenfield
May 28 '13 at 0:28
@android_developer (5 comments above)
dp does not have the exact same physical length. (Although it is close.) See @Fraggle's comment about bucketing. What this means is that 48dp will be roughly 8mm (0.3 inch), but it may vary up to 11mm.– intrepidis
Jun 15 '13 at 6:59
dp
The reason for bucketing is so that developers can test their apps on a few devices of different densities and be confident the the layouts will look the same on a multitude of devices. So even if the physical size of buttons etc changes a little bit, the overall look of an activity will be the same.
– intrepidis
Jun 15 '13 at 7:03
Pretty much everything about this and how to achieve the best support for multiple screens with different sizes and densities is very well documented here:
Screen size
Actual physical size, measured as the screen's diagonal.
For simplicity, Android groups all actual screen sizes into four
generalized sizes: small, normal, large, and extra-large.
Screen density
The quantity of pixels within a physical area of the
screen; usually referred to as dpi (dots per inch). For example, a
"low" density screen has fewer pixels within a given physical area,
compared to a "normal" or "high" density screen. For simplicity,
Android groups all actual screen densities into six generalized
densities: low, medium, high, extra-high, extra-extra-high, and
extra-extra-extra-high.
Orientation
The orientation of the screen from the user's point of
view. This is either landscape or portrait, meaning that the screen's
aspect ratio is either wide or tall, respectively. Be aware that not
only do different devices operate in different orientations by
default, but the orientation can change at runtime when the user
rotates the device.
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.
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.
px = dp * (dpi / 160)
If you are any serious about developing an Android app for more than one type of device, you should have read the screens support development document at least once. In addition to that it is always a good thing to know the actual number of active devices that have a particular screen configuration.
So if you use dp for a button and sp for the font size of the button text, what happens when the user starts scaling? The text will enlarge, but will the button accommodate this by enlarging also?
– Wytze
Oct 5 '12 at 9:35
@Wytze, nope. For things where fitting the text may be an issue, I would just use dip so things don't overflow.
– eski
Jul 13 '13 at 17:39
@Wytze And I, on the contrary, use sp for both the text and whatever it contains. For example, if I have a button with a fixed size and a text inside it, I would assign the button size in sp so it scales up when needed too. Otherwise, the user will be annoyed that he increased the size of the text, and it didn't increase. Of course, the layout should be flexible enough to allow it.
– Malcolm
Oct 14 '13 at 23:15
@Sam Well, it's up to you to decide what is more important: your design or the ability for users with vision problems to read your text.
– Malcolm
Mar 28 '14 at 2:13
The top answer puts it much better than I can. Final point - try telling a designer he needs to leave lots of dead space in his views so that the text can grow/shrink...
– Sam
Mar 28 '14 at 10:27
I will elaborate more on how exactly does dp convert to px:
150 x 150 px
150 * 150 dp
150 x 150 px
100 * 100 dp
150x150 px
75 * 75 dp
The other way around: say, you want to add an image to your application and you need it to fill a 100 * 100 dp control. You'll need to create different size images for supported screen sizes:
100 * 100 dp
100 * 100 px
150 * 150 px
200 * 200 px
How do we calculate the pixels for hdpi,ldpi,mdpi etc.. I heard we use this formula..px = dp * ( dpi / 160 ); can you explain me with this formula plz ?
– Rakesh patanga
Mar 7 '15 at 12:25
@Rakeshpatanga At a density of 160 dpi, 1 px = 1 dp (the physical pixel and dp abstraction are the same). That is, for a single dp we have (1)*(160/160) = 1, or exactly 1 px. At the higher density of 320 dpi, a single dp is (1)*(320/160) = 2 pixels, and two dp's are (2)*(320/160) = 4 px.
– samsara
Sep 6 '17 at 14:32
Line these up in monospace to see: |1dp|, |___|___| 160, |_|_|_|_| 320 (put line breaks after the commas). |_| = 1 pixel.
– samsara
Sep 6 '17 at 14:49
re "You'll need to create different size images for supported screen sizes:" That depends. For small high-contrast images, e.g. icons, it is wise to create at least the smallest size (to be sure it will be clear) plus a fairly large size (so that large versions don't look blurry). But even for icons, you can allow one image to scale over a range of sizes, at higher densities. For photographs, just specify the display area you want it to fill, and rely on the device scaling.
– ToolmakerSteve
May 22 at 11:51
px
Pixels - point per scale 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.
dp
dip
dp
dp
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.
dp
Take the example of two screens that are the same size but one has a screen density of 160 dpi (dots per inch, i.e. pixels per inch) and the other is 240 dpi.
Lower resolution screen Higher resolution, same size
Physical Width 1.5 inches 1.5 inches
Dots Per Inch (“dpi”) 160 240
Pixels (=width*dpi) 240 360
Density (factor of baseline 160) 1.0 1.5
Density-independent Pixels 240 240
(“dip” or “dp” or “dps”)
Scale-independent pixels
(“sip” or “sp”) Depends on user font size settings same
Moreover you should have clear understanding about the following concepts:
Screen size:
Actual physical size, measured as the screen's diagonal. For simplicity, Android groups all actual screen sizes into
four generalized sizes: small, normal, large, and extra large.
Screen density:
The quantity of pixels within a physical area of the screen; usually referred to as dpi (dots per inch). For example, a
"low" density screen has fewer pixels within a given physical area,
compared to a "normal" or "high" density screen. For simplicity,
Android groups all actual screen densities into four generalized
densities: low, medium, high, and extra high.
Orientation:
The orientation of the screen from the user's point of view. This is either landscape or portrait, meaning that the
screen's aspect ratio is either wide or tall, respectively. Be aware
that not only do different devices operate in different orientations
by default, but the orientation can change at runtime when the user
rotates the device.
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.
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.
Reference: Android developers site
dp is dip. Use it for everything (margin, padding, etc.).
dp
dip
Use sp for {text-size} only.
sp
See the difference between px, dp and sp on different screen sizes.
px
dp
sp

Source: Android Programming: The Big Nerd Ranch Guide
But I can't pass dp value to setX or setY function, android doesn't translate it here. What can I do?
– Daniel Viaño
Jun 22 '16 at 16:25
I have calculated the formula below to make the conversions dpi to dp and sp
dpi
dp
sp
It should be ppi instead of dpi
– Prateek
Jan 29 '14 at 17:02
Definitions
px or dot is a pixel on the physical screen.
dpi are pixels per inch on the physical screen and represent the density of the display.
Android gives alias names to several densities
dip or dp are density-indenpendant pixels, i.e. they correspond to more or less pixels depending on the physical density.

sp or sip is a scale-independant pixel. They are scaled when the Large Text option is turned on in Settings > Accessibility
What to use?
Use sp for Text size.
Use dp for everything else.
Source 1
Source 2
Source 3: (data from source 3 is given below)
These are dimension values defined in XML. A dimension is specified
with a number followed by a unit of measure. For example: 10px, 2in,
5sp. The following units of measure are supported by Android:
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.
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 the user's preference.
pt
Points - 1/72 of an inch based on the physical size of the screen.
px
Pixels - Corresponds to actual pixels on the screen. This unit of
measure is not recommended because the actual representation can vary
across devices; each devices may have a different number of pixels per
inch and may have more or fewer total pixels available on the screen.
mm
Millimeters - Based on the physical size of the screen.
in
Inches - Based on the physical size of the screen.
Note: A dimension is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine dimension resources with other simple resources in the one XML file, under one element.
Also: what is the added value of this answer? There doesn't seem to be anything that hasn't already been mentioned in other answers.
– laalto
Dec 27 '13 at 9:28
Basically the only time where px applies is one px, and that's if you want exactly one pixel on the screen like in the case of a divider:
On >160 dpi, you may get 2-3 pixels,
On >120 dpi, it rounds to 0.
getDimensionPixelOffset rounds down, getDimensionPixelSize rounds up.
– Eugen Pechanec
May 28 '17 at 22:24
px
Pixels - corresponds to actual pixels on the screen.
dp or dip
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.
Use of dp:
Density independence -
Your application achieves “density independence” when it preserves the physical size (from the user’s point of view) of user interface elements when displayed on screens with different densities. (ie) The image should look the same size (not enlarged or shrinked) in different types of screens.
sp
Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference.
http://developer.android.com/guide/topics/resources/more-resources.html#Dimension
I see that this is a late answer to a big question. May I ask what this answer provides that none of the other answers already do?
– Mysticial
Apr 2 '13 at 6:39
A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way. As described above, 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.
Understanding pixel to dp and vice versa is very essential (especially for giving exact dp values to creative team)
dp = px * 160 / dpi
MDPI = 160 dpi || Therefore, on MDPI 1 px = 1 dp
For example, if you want to convert 20 pixel to dp, use the above formula,
dp = 20 * 160 / 160 = 20.
So, 20 pixel = 20 dp.
HDPI = 240 dpi - So, on HDPI 1.5 px = 1 dp
XHDPI = 320 dpi - So, on XHDPI 2 px = 1 dp
XXHDPI = 480 dpi - So, on XXHDPI 3 px = 1 dp
For example, let us consider Nexus 4.
If 24 pixels to be converted to dp and if it is a Nexus 4 screen, developers can
convert it to dp easily by the following calculation :
dp = 24 * 160 / 320 = 12 dp
Screen dimension:
768 x 1280 pixel resolution (320 ppi or 320dpi)
Optional (screen size):
4.7" diagonal
It is explained above. Try to avoid in layout files. But there are some cases, where px is required. for example, ListView divider line. px is better here for giving a one-pixel line as a divider for all across screen resolutions.
Use sp for font sizes. Then only the font inside the application will change while device fonts size changes (that is, Display -> Fonts on Device). If you want to keep a static sized font inside the app, you can give the font dimension in dp. In such a case, it will never change. Developers may get such a requirement for some specific screens, for that, developers can use dp instead of sp. In all other cases, sp is recommended.
You can see the difference between px and dp from the below picture, and you can also find that the px and dp could not guarantee the same physical sizes on the different screens.
px
dp
px
dp

@EnesBattal, I think because the dp isn't an acute physical size, it is a approximate value. Quoting from CapTech : "dp - This is a density independent unit, however the physical size of a single “dp” is only approximately the same on every screen density. There are approximately 160 “dp” in an inch. A scaling factor, depending on the density bucket of the device, is applied to convert “dp” to the number of pixels at 160 dpi. The number of pixels a single “dp” translates to varies depending on the pixel on screen density and the density bucket the device falls into."
– Zephyr
Sep 17 '15 at 14:12
@RuchirBaronia, I think the DP or DIP is still there inside apk, because the apk doesn't know which kind of screen density it will run with yet, so the device independence should be still kept.
– Zephyr
Nov 25 '15 at 18:18
it is not a square!... It's a bar!!!
– Ram
Mar 21 '17 at 14:02
Anything related with the size of text and appearance must use sp or pt. Whereas, anything related to the size of the controls, the layouts, etc. must be used with dp.
sp
pt
dp
You can use both dp and dip at its places.
dp
dip
I would only use dp.
There is a lot of talk about using "sp" for font sizes, and while I appreciate the point, I don't think that it is the right thing to do from a design point of view. You can end up breaking your design if the user has some wonky font size selection, and the user will end up blaming the app, and not their own life choices.
Also, if you take an sp-font app on a 160 dpi tablet, you will find that everything scales up... but your font, which is going to look tiny in comparison. It isn't a good look.
While the idea of "sp" fonts has a good heart, it is a poor idea. Stick with dp for everything.
You know that the font scale factor applied to
sp is a factor, right? Anything that affects dp will also affect sp. That said, it's still preferable to specify font sizes using dp instead of sp if your layout is very tight and the larger sizes won't fit - better to have text smaller than what the user wants that a completely messed up layout. But in the first instance you should always strive to respect the user's font size preference - even the biggest setting is not THAT big.– Karu
Feb 26 '17 at 22:06
sp
dp
sp
dp
sp
Difference between dp and sp units mentioned as "user's font size preference" by the answers copied from official documentation can be seen at run time by changing Settings->Accessibility->Large Text option.
dp
sp
Settings->Accessibility->Large Text
Large Text option forces text to become 1.3 times bigger.
Large Text
1.3
private static final float LARGE_FONT_SCALE = 1.3f;
This might be well of course vendor dependent since it lies in packages/apps/Settings.
sp = scale independent pixel
dp = dip = density independent pixels
dpi = dots per inch
We should avoid to use sp.
We should use dp to support multiple screens.
Android supports different screen resolutions
An 120 dp ldpi device has 120 pixels in 1 inch size.
The same for other densities...
We as software engineers should use this conversion formulae:
pixel = dp * (density / 160)
So 240 dpi device's 1 dp will have = 1 * (240/160) = 3/2 = 1.5 pixels.
And 480 dpi device's 1 dp will have = 1 * (480/160) = 3 pixels.
Using this 1.5 and 3 pixels knowledge, a software engineer can design layouts for different densities.
To check screen parameters of any device:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Toast.makeText(
this,
"4:" + metrics.heightPixels + "," + metrics.density + ","
+ metrics.densityDpi, Toast.LENGTH_LONG).show();
Very good tutorial for understanding is : http:/vinsol.com/blog/2014/11/20/tips-for-designers-from-a-developer
– Kushal
Mar 16 '15 at 10:06
"We should avoid to use sp" Why is that? You should use sp when dealing with font sizes because it takes into account the user's preferred text size, developer.android.com/training/multiscreen/screendensities.html
– ci_
Mar 20 '15 at 10:05
I have answered relative to layout perspective.. Please read on link you provided "so you should use this measurement unit when defining text size (but never for layout sizes)."
– Kushal
Mar 20 '15 at 10:22
The question was "Difference between px, dp, dip and sp in Android?" your answer said "We should avoid to use sp". There was no mention of "layout perspective" anywhere.
– ci_
Mar 20 '15 at 10:28
Yes.. dp and dip are same... used interchangeably... Is my answer solve your query?
– Kushal
Nov 24 '15 at 6:36
dpi -
px - pixel
pt - points
in - inch
- with respect to physical screen size(1 inch = 2.54 cm).
mm- milimeter
- with respect to physical screen size.
sp - scale-independent pixel.
dip -
In standard, dp and sp are used. sp for font size and dp for everything else.
Formula for conversion of units:
px = dp * ( dpi / 160 );
Density Bucket -> Screen Display => Physical Size => Pixel Size
ldpi -> 120 dpi => 0.5 x 0.5 in => 0.5 in * 120 dpi = 60x60 px
mdpi -> 160 dpi => 0.5 x 0.5 in => 0.5 in * 160 dpi = 80x80 px
hdpi -> 240 dpi => 0.5 x 0.5 in => 0.5 in * 240 dpi = 120x120 px
xhdpi -> 320 dpi => 0.5 x 0.5 in => 0.5 in * 320 dpi = 160x160 px
xxhdpi -> 480 dpi => 0.5 x 0.5 in => 0.5 in * 480 dpi = 240x240 px
xxxhdpi -> 640 dpi => 0.5 x 0.5 in => 0.5 in * 640 dpi = 320x320 px
As per the documentation The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp”.
– Arunendra
Nov 24 '15 at 8:15
Normally sp is used for font sizes, while dip is used (also called dp) for others.
Screen Size in Android is grouped into categories small, medium, large, extra large, double-extra and triple-extra. Screen density is the amount of pixels within an area (like inch) of the screen. Generally it is measured in dots-per-inch (dpi). Screen density is grouped as low, medium, high and extra high. Resolution is the total number of pixels in the screen.
Android
small
medium
large
extra large
double-extra
triple-extra
Formula for Conversion between Units
px = dp * (dpi / 160)
dp to px in device
Following example may help understand better. The scaling occurs based on bucket size of 120(ldpi), 160(mdpi), 240(hdpi), 320(xhdpi), 480(xxhdpi) and 640(xxxhdpi). The Google suggested ratio for designing is 3:4:6:8:12 for ldpi:mdpi:hdpi:xhdpi:xxhdpi
A 150px X 150px image will occupy,
You may use the following DPI calculator to fix your image sizes and other dimensions when you wish to have an uniform UI design in all Android devices.
DPI Calculator in Java
/*
Program output
LDPI: 165.0 X 60.0
MDPI: 220.0 X 80.0
HDPI: 330.0 X 120.0
XHDPI: 440.0 X 160.0
XXHDPI: 660.0 X 240.0
XXXHDPI: 880.0 X 320.0
*/
public class DPICalculator {
private final float LDPI = 120;
private final float MDPI = 160;
private final float HDPI = 240;
private final float XHDPI = 320;
private final float XXHDPI = 480;
private final float XXXHDPI = 640;
private float forDeviceDensity;
private float width;
private float height;
public DPICalculator(float forDeviceDensity, float width, float height){
this.forDeviceDensity = forDeviceDensity;
this.width = width;
this.height = height;
}
public static void main(String... args) {
DPICalculator dpiCalculator = new DPICalculator(240,330,120);
dpiCalculator.calculateDPI();
}
private float getPx(float dp, float value) {
float px = dp * (value / forDeviceDensity );
return px;
}
private void calculateDPI() {
float ldpiW = getPx(LDPI,width);
float ldpiH = getPx(LDPI,height);
float mdpiW = getPx(MDPI,width);
float mdpiH = getPx(MDPI,height);
float hdpiW = getPx(HDPI,width);
float hdpiH = getPx(HDPI,height);
float xdpiW = getPx(XHDPI,width);
float xdpiH = getPx(XHDPI,height);
float xxdpiW = getPx(XXHDPI,width);
float xxdpiH = getPx(XXHDPI,height);
float xxxdpiW = getPx(XXXHDPI,width);
float xxxdpiH = getPx(XXXHDPI,height);
System.out.println("LDPI: " + ldpiW + " X " + ldpiH);
System.out.println("MDPI: " + mdpiW + " X " + mdpiH);
System.out.println("HDPI: " + hdpiW + " X " + hdpiH);
System.out.println("XHDPI: " + xdpiW + " X " + xdpiH);
System.out.println("XXHDPI: " + xxdpiW + " X " + xxdpiH);
System.out.println("XXXHDPI: " + xxxdpiW + " X " + xxxdpiH);
}
}
More Information refer following link.
http://javapapers.com/android/difference-between-dp-dip-sp-px-in-mm-pt-in-android/
Here's the formula used by Android:
px = dp * (dpi / 160)
Where dpi is one of the following screen densities. For a list of all possible densities go here
It defines the "DENSITY_*" constants.
Taken from here.
This will sort out a lot of the confusion when translating between px and dp, if you know your screen dpi.
So, let's say you want an image of 60 dp for an hdpi screen then the physical pixel size of 60 dp is:
px = 60 * (240 / 160)
sp = scale independent pixel
dp = density independent pixels
dpi = density pixels
I have gone through the above answers...not finding them exactly correct.
sp for text size, dp for layout bounds - standard.
But sp for text size will break the layout if used carelessly in most of the devices.
sp take the textsize of the device, whereas dp take that of device density standard( never change in a device)
Say 100sp text can occupies 80% of screen or 100% of screen depending on the font size set in device

You can use sp for layout bounds also, it will work :)
No standard app use sp for whole text
Use sp and dp for text size considering UX.
Some people use huge FONT in their phone for more readability, giving them small hardcoded sized text will be an UX issue. Put sp for text where necessary, but make sure it won't break the layout.
I've come across a good article about designing Android apps UI for different screen resolutions, and I'd like to leave it here just for somebody searching in this area. Yes, I know that it's somehow described in Google docs (and mentioned in the posts above), I read that but it was not good for me (yeah, I may be too stupid)). It remained unclear for me how to design layouts capable to handle different screen size. I hate DP concept and so on, when I need to implement a "flexible" UI layout for different screens. (Hey iOS developers - yes, you're right it's Storyboard concept).
Android has not bad UI concept, but lacks iOS Storyboard features, unfortunately. Designing flexible UI in Android is not easy thing (at the best).
Here goes the article that helped me to understand what to do in Android to make layouts for different screen sizes:
JMSTUDIO Blog :- Decide Android App Screen Size
How to Design UI for Android Apps for Different Screen Size
To design an app UI for different screen sizes, our initial design has to
meet a minimum required space for each screen size. Android defines a
minimum size (in dp) for each generalized screen type. Here is an
Android screen size guideline.

When we get the screen size in dp, it is not enough for us to design
the Android app UI. For each screen size, we need to prepare graphics
and bitmap images for each density. Here is an Android screen density
guideline.

For easy calculation, we can follow the 3:4:6:8 scaling ratio between
the four generalized densities. If we create a 36×36 pixel picture for
ldpi device, the rest densities pictures size will be 48×48 for mdpi,
72×72 for hdpi, and 96×96 for xhdpi.
How to Design Android Apps UI in Photoshop
Many designers have problems for designing Android app UI in photoshop or other pixel
based graphic design tools because of density-independent unit, dp.
Designers don’t know how to map dp to pixel. Google also doesn’t give
a clear Android UI design guide for them, though they give a basic
formula for dp and pixel translation.
As Android’s definition, 1pd equal to 1px under 160 dpi device (mdpi).
So we want to design an Android app for xlarge Android device with
mdpi density, we can define our UI size in pixel as 960 pixel in width
and 720px in height; Follow the same mapping rule, we can get
following Android App screen size UI design guideline:

ADDED: If you interested in "flexible" UI too, have a look at this library: An Android SDK that provides a new size unit - sdp (scalable dp). This size unit scales with the screen size (this also mentioned in an answer here, about SDP library)
SDP
ADDED2 Google has finally understood usefulness of iOS Storeboard UI concept, and here goes ConstraintLayout for Android world: Build a Responsive UI with ConstraintLayout (IMHO a must to read for every Android dev).
ConstraintLayout
Screen size in Android is grouped into categories ldpi, mdpi, hdpi, xhdpi, xxhdpi and xxxhdpi. Screen density is the amount of pixels within an area (like inch) of the screen. Generally it is measured in dots-per-inch (dpi).
ldpi
mdpi
hdpi
xhdpi
xxhdpi
xxxhdpi
dpi
PX(Pixels):
PX(Pixels):
px
DP/DIP(Density pixels / Density independent pixels):
DP/DIP(Density pixels / Density independent pixels):
dip == dp. In earlier Android versions dip was used and later changed to dp. This is alternative of px.
dip == dp
dp
px
Generally we never use px because it is absolute value. If you use px to set width or height, and if that application is being downloaded into different screen sized devices, then that view will not stretch as per the screen original size.
px
px
dp is highly recommended to use in place of px. Use dp if you want to mention width and height to grow & shrink dynamically based on screen sizes.
dp
px
dp
if we give dp/dip, android will automatically calculate the pixel size on the basis of 160 pixel sized screen.
dp/dip
SP(Scale independent pixels):
SP(Scale independent pixels):
scaled based on user’s font size preference. Fonts should use sp.
sp
when mentioning the font sizes to fit for various screen sizes, use sp. This is similar to dp.Use sp especially for font sizes to grow & shrink dynamically based on screen sizes
sp
dp
sp
Android Documentation says:
when specifying dimensions, always use either dp or sp units. A dp is
a density-independent pixel that corresponds to the physical size of a
pixel at 160 dpi. An sp is the same base unit, but is scaled by the
user's preferred text size (it’s a scale-independent pixel), so you
should use this measurement unit when defining text size
dp
sp
dp
dpi
sp
1) dp: (density independent pixels)
dp: (density independent pixels)
The number of pixels represented in one unit of dp will increase as the screen resolution increases (when you have more dots/pixels per inch). Conversely on devices with lower resolution, the number of pixels represented in on unit of dp will decrease. Since this is a relative unit, it needs to have a baseline to be compared with. This baseline is a 160 dpi screen. This is the equation: px = dp * (dpi / 160).
px = dp * (dpi / 160).
2) sp: (scale independent pixels)
sp: (scale independent pixels)
This unit scales according to the screen dpi (similar to dp) as well as the user’s font size preference.
3) px: (pixels)
px: (pixels)
Actual pixels or dots on the screen.
For more details you can visit
Android Developer Guide > Dimension
Android Developer Guide > Screens
The screen of a mobile phone is made up of thousands of tiny dots known as pixels (px). A pixel is the smallest element which goes to make the picture. The more the number of pixels to make a picture or wording, the sharper it becomes and makes the smartphone screen more easily readable.
Screen resolution is measured in terms of number of pixels on the screen. Screen resolution is a commonly-used specification when buying a device, but it's actually not that useful when designing for Android because thinking of screens in terms of pixels ignores the notion of physical size, which for a touch device is really really important.
Density independent pixel (dp or dip) allow the designer to create assets that appear in a expected way, no matter the resolution or density of target device.
A density independent pixel (dp or dip) is equal to one pixel at the baseline density or 160 dpi (dots per inch).
1 px/1dp = 160 dpi/160 dpi
2 px/1dp = 320 dpi(2x)/160 dpi
where,
dpi is dots per inch
So, at 320 dpi, 1 dp is equal to 2 px.
Formula
px/dp = dpi/160dpi
Dots per inch (dpi) is a measure of the sharpness (that is, the density of illuminated points) on a display screen. The dots per inch for a given picture resolution will differ based on the overall screen size since the same number of pixels are being spread out over a different space.
Working with density independent pixels help us to deal with a situation like where you have two devices with same pixel resolution, but differing amount of space. Suppose in a case, a tablet and phone has the same pixel resolution 1280 by 800 pixels (160 dpi) and 800 by 1280 pixels (320 dpi) respectively.
Now because a tablet is at baseline density (160 dpi) its physical and density independent pixels sizes are the same, 1280 by 800. The phone on the other hand has a higher pixel density, so it has half as many density independent pixels as physical pixels. So a phone has 400 by 640 density independent pixels. So using a density-independent pixel makes it easier to mentally picture that tablet has much more space than the phone.
Similarly, if you have two devices with similar screen size, but different pixel density, say one is 800 by 1280 pixels (320 dpi), and the other is 400 by 640 pixels (160 dpi), we don't need to define totally different layouts for these two devices as we can measure assets in terms of density independent pixel which is same for both devices.
800 by 1280 pixels (320dpi)=400 by 640 density independent pixel (dp)
400 by 640 pixels (160 dpi)=400 by 640 density independent pixel (dp)
Scale independent pixels(sp) is the preferred unit for font size.
For accessibility purposes, Android allows users to customize their device's font size. Users that have trouble reading text can increase their device's font size. You can normally find this option in the display setting on your phone or tablet under font size. It's often also available through the accessibility settings.
With scale independent pixels, 16 sp is exactly the same as 16 dp when the device's font size is normal or 100%. But when device's font size is large, for example 125%, 16 sp will translate to 20 dp or 1.25 times 16.
If you use dp as the unit for font size, then that piece of text has a specific physical size no matter if the user has customize device's font size. Using sp units will make a better experience for people with impaired eyesight.
Reference: Udacity, Google
sp: scale independent pixel
You should use it with texts because it is automatically scaled according to the font size that is being used by the user in his device.
px: pixel or picture element is the single point on the screen
Your answer is not only incomplete but also was answered way before you even saw the question.
– Shiro
Jul 1 '16 at 17:15
I want to provide an easy way to understand dp. In fact, I think dp is the easiest one to understand. dp is just a physical length unit. It's of the same dimension as mm or inch. It's just convenient for us to write 50dp, 60dp rather than 50/160 inch or 60/160 inch, because one dp is just 1/160 inch whatever the screen size or resolution is.
dp
dp
dp
mm
inch
50dp
60dp
50/160 inch
60/160 inch
dp
1/160 inch
The only problem is that, the android dpi of some screens are not accurate. For example, a screen classified to 160dpi may have 170dpi indeed. So the computation result of dp is fuzzy. It should be approximately the same as 1/160 inch.
dp
1/160 inch
SDP - a scalable size unit - basically it is not a unit, but dimension resources for different screen size.
Try the sdp library from Intuit. It's very handy to solve unit problems, and you can quickly support multiple screens.
Usage
android:paddingBottom="@dimen/_15sdp" for positive and android:layout_marginTop="@dimen/_minus10sdp" for negative sdp sdp
android:paddingBottom="@dimen/_15sdp"
android:layout_marginTop="@dimen/_minus10sdp"
It has equivalent value in dp for each size in values-sw<N>dp folders (sw = smallestWidth).
values-sw<N>dp
Attention
Use it carefully! In most cases you still need to design a different layout for tablets.
Example
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_minus10sdp"
android:paddingBottom="@dimen/_15sdp"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:includeFontPadding="false"
android:text="♡"
android:textColor="#ED6C27"
android:textSize="@dimen/_70sdp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:includeFontPadding="false"
android:text="U"
android:textColor="@android:color/black"
android:textSize="@dimen/_70sdp" />
</LinearLayout>
You can use db for text size, but I prefer ssp for text size.
For more details, check the library GitHub page.
Before answering this question let me decrease the number of units first. So here you go: dp or dip are both the same and are known as Density-independent pixels.
1. px - stands for pixels. Pixels are a single dot, point on a screen. Generally in the mobile industry it is measured in ppi (pixels per inch). Screen resolution is directly proportional to ppi, the larger the number of pixels per inch the higher the screen resolution.
For example, if you draw an image of a size 200 px * 200 px, then its appearance must be different on a high-resolution device versus a low-resolution device. The reason is a 200 px image on a low-resolution phone will be look larger than on a high-resolution device.
Below images are showing a resolution of the same image on different phones -
Phone with High screen resolution

Phone with Low screen resolution

2. dip or dp - 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. "Density independence" refers to the uniform display of UI elements on screens with different densities.

A dp is equal to one physical pixel on a screen with a density of 160. To calculate dp:
dp = (width in pixels * 160) / screen density
3. sp - stands for scalable pixels. Generally sp is used for texts on the UI, and sp preserves the font settings. For example, if a user selected a larger font than 30 sp it will auto scale to appear large according to a user preference.
you confused with the
Phone with low screen resolution to the appropriate image– Royi Namir
Dec 4 '17 at 12:30
Phone with low screen resolution
@RoyiNamir Could you please explain your comment, or if possible please suggest edit
– Rahul
Dec 7 '17 at 13:23
this is a phone with HIgh resolution. not low : i.imgur.com/tnu87VR.jpg
– Royi Namir
Dec 7 '17 at 13:24
@RoyiNamir Thank you for noticing and update. :)
– Rahul
Dec 25 '17 at 17:16
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
this is use full difference between px, dip, dp and sp in android [ developer.android.com/guide/topics/resources/…
– NagarjunaReddy
May 25 '12 at 9:34