Android dp and px: what is it about

What is it

While programing GUI on Android one should mostly define dimensions in dp (density independent pixels) instead of px (pixels). The main difference between both is:

dp will always have the same size on every smartphone screen. px is not always the same dimensions. It is well visualized on screenshots on Android API Guides, in section Density independence – note images showing results of using x vs. dp on various screens.

px size depends on screen density (or screen resolution). Density (dpi) defines how many pixels will fit in one inch on the screen. Higher density means that there will be more pixels on one inch than on the screen with lower density.

How to use it

Relation of dp, px and dpi is:

px = dp * (dpi / 160)

That means that the definition is as follows: One pixel is equal to one dp on 160 dpi screen.

Each device screen has its dpi value. For example HTC Desire HD dpi value is 255 dpi. For example, for HTC Desire HD screen, values’ relations between dp and px is presented in table below. These are font sizes fo TextView.

px value dp value what is it?
28,7 18 large font size
25,5 16 medium font size
22,3 14 small font size
19,1 12 micro font size

What is it useful for

This is necessary while developing GUI, and while designing it in some vector graphic tools (Adobe Illustrator, Inkscape), where only pixels can be used.

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!

4 thoughts on “Android dp and px: what is it about

  1. You would use Adobe Illustrator to draw vector graphics such as icons and UI components. Adobe InDesign is a document layout tool for publishing and online media.

    1. You’re right. Very valuable input – thank You!

      I’ve just did not remember well what my GUI designer told me :P

Give Your feedback: