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 commonly used spec 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 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
2px/1dp = 320 dpi(2x)/160 dpi
dpi is dots per inch
So, At 320 dpi, 1dp is equal to 2 px.
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 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 same pixel resolution 1280 by 800 pixels(160 dpi) and 800 by 1280 pixels(320 dpi) respectively. Now because 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 phone has 400 by 640 density independent pixels.So using 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 unit for font size,than that piece of text has 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.