Difference Between dp, dip, sp, px, in, mm, pt in Android

This Android tutorial is to explain the difference between dp, dip, sp, px, in, mm, pt and what we should use when. This is to enable design a consistent UI across different Android screen sizes. Before going into the difference let us understand some basic terminologies. 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.

Always use dp and sp only. sp for font sizes and dp for everything else. It will make UI compatible for Android devices with different densities.

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.

Note: When designing an image for Android in Photoshop or other image editing tools, have the resolution as 72 pixels/inch.

Android Launcher Icon Sizes

Following are the launcher icon sizes in pixels for various 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(){  }

    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);        
    }
}

This Android tutorial was added on 15/07/2014.

Comments on "Difference Between dp, dip, sp, px, in, mm, pt in Android" Tutorial:

  1. […] This Android DPI calculator is to help build a uniform UI in Android apps. If you want to know more about this, go through difference Between dp, dip, sp, px, in, mm, pt in Android. […]

Comments are closed for this "Difference Between dp, dip, sp, px, in, mm, pt in Android" tutorial.