SoFunction
Updated on 2025-03-11

Detailed explanation of the differences between dip, dp, sp, pt and px in Android

1. Overview
In the past, programmers usually designed computer user interfaces in pixels. For example: the image size is 80×32 pixels. The problem with this is that if you run the program on a new monitor with higher dots per inch (dpi), the user interface will appear small. In some cases, the user interface may be as small as it is difficult to see the content clearly. This can solve this problem by using units of measurement that are independent of resolution to develop programs. Android application development supports different units of measurement.

2. Meaning of unit of measurement
dip: device independent pixels. Different devices have different display effects, which is related to the device hardware. Generally, we recommend using this to support WVGA, HVGA and QVGA, and do not rely on pixels.
dp: dip is the same
px: pixels. Different devices have the same display effect. Generally, our HVGA represents 320x480 pixels, which is used more frequently.
pt: point, is a standard unit of length, 1pt = 1/72 inch, used in the printing industry, very simple and easy to use;
sp: scaled pixels. It is mainly used for font display best for textsize.
in (inch): length unit.
mm (mm): length unit.

3. Conversion formula for unit of measurement
In the Android source code package, we look at the following functions:

Copy the codeThe code is as follows:

  public static float applyDimension(int unit, float value,
   DisplayMetrics metrics)
   {
 switch (unit) {
 case COMPLEX_UNIT_PX:
     return value;
 case COMPLEX_UNIT_DIP:
     return value * ;
 case COMPLEX_UNIT_SP:
     return value * ;
 case COMPLEX_UNIT_PT:
     return value * * (1.0f/72);
 case COMPLEX_UNIT_IN:
     return value * ;
 case COMPLEX_UNIT_MM:
     return value * * (1.0f/25.4f);
 }
 return 0;
    }

 This function function: converts each unit into pixels.
: The default value is DENSITY_DEVICE / (float) DENSITY_DEFAULT;
: The default value is DENSITY_DEVICE / (float) DENSITY_DEFAULT;
: The default value is DENSITY_DEVICE;
DENSITY_DEVICE: is the screen density
DENSITY_DEFAULT: The default value is 160

4. Screen density: It indicates how many display points there are per inch, which are two different concepts from resolution.
Android mainly has the following types of screens: the following table

screen

Tyep

width

Pixels

high

Pixels

size

Range(inches)

Screen density

QVGA

240

320

2.6-3.0

low

WQVGA

240

400

3.2-3.5

low

FWQVGA 

240

432

3.5-3.8

low

HVGA 

320

480

3.0-3.5

Medium

WVGA 

480

800

3.3-4.0

High

FWVGA

480

854

3.5-4.0

High

WVGA

480

800

4.8-5.5

Medium

FWVGA 

480

854

5.0-5.8

Medium

Remark

Currently, the default low=120 for Android; Medium=160; High=240


5. To sum up   
According to px = dip * density / 160, then when the screen density is 160, px = dip
According to Google's suggestion, it is best to use sp as the unit for the font size of TextView. If you look at the source code of TextView, you can see that Android uses sp as the font size unit by default. Use dip as a unit of other elements.