Android dp and px: what is it about

28 07 2012

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 responses

1 08 2012
Andy Res

Also, there are some online calculators that may help you in dp/px calculations.
I prefer this one:

28 11 2012
18 09 2012
Cheryl Lynn Taylor

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.

18 09 2012
Jacek Milewski

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:

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: