7

I am new to android. I read about DP but m still confused. In one definition it says- dp (density-independent pixels): An abstract unit based on the density of the screen. On a display with 160 dots per inch, 1dp = 1px.

does it mean- 160 dots=1 dp = 1 px (each dot is 1 pixel , right?) OR 1 dp = 1 dot(pixel) among the 160 dots

Pleas clarify

Chirag
  • 56,621
  • 29
  • 151
  • 198
Tanvir
  • 1,642
  • 8
  • 32
  • 53

2 Answers2

14

density-independent pixels is a virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way.

The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple:

px = dp * (dpi / 160)

For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.

For 160 dpi screen 1 dp equals 1 px.

Refer to this blog and this answer.

Community
  • 1
  • 1
Jainendra
  • 24,713
  • 30
  • 122
  • 169
0

The android documentation say -

The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, the baseline density assumed by the platform (as described later in this document). At run time, the platform transparently handles any scaling of the dp units needed, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: pixels = dps * (density / 160). For example, on 240 dpi screen, 1 dp would equal 1.5 physical pixels. Using dp units to define your application’s UI is highly recommended, as a way of ensuring proper display of your UI on different screens.

you may take a look at the supporting multiple screens. you may also take a look at this question.

Community
  • 1
  • 1
darsh
  • 741
  • 4
  • 10
  • 34
  • what is DOT in Dot per inch, I assume 1 dot=1 px, is it right? This line confuses me- one physical pixel on a 160 dpi screen WHEREAS it says again -on 240 dpi screen, 1 dp would equal 1.5 physical pixels. how come 1 dp would equal 1.5 physical pixels here.. – Tanvir Sep 01 '12 at 05:28