2

i have screen which is deisgned in XHDPI .

I have to convert all padding ,margin value of dp into LDPI,MDPI,HDPI,XXHDPI,XXXHDPI

Can any one please suggest me how i will calculate dp value for follwing screen suppose i have paddingTop in xhdpi is 56dp then what value we have give for ldpi,mdpi, xxhdpi and xxxhdpi

please suggest me.

Nawrez
  • 3,314
  • 8
  • 28
  • 42
Manoj Kumar
  • 61
  • 1
  • 9

1 Answers1

5

Density-independent pixel (dp)

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.

If you want to know what's 56dp in xhdpi, that's 56dp in ldpi, mdpi, hdpi, xxhdpi, xxxhdpi as well - as the definition says, it's density-independent pixels.

When you work with a design, you want to know one thing:

What's the baseline scale for the design?

Some quick answers: It's usually 1x, 2x or in rare cases 3x

Android defines the following densities:

  • ldpi (low) ~120dpi
  • mdpi (medium) ~160dpi
  • hdpi (high) ~240dpi
  • xhdpi (extra-high) ~320dpi
  • xxhdpi (extra-extra-high) ~480dpi
  • xxxhdpi (extra-extra-extra-high) ~640dpi

There are some rare cases in-between, like tvdpi, but in 99% of the cases you can just simply ignore those.

When designers say 1x, they usually refer to mdpi, 2x (or retina) refers to xhdpi and 3x design is done in xxhdpi.

Once you know the baseline, you know how to translate the pixel values from the design document to dip (density-independent pixel).

The formula is quite simple, but if you doubt yourself, feel free to try one of the online converter tools:

The formula is as follows: dp = px / (dpi / 160) where dpi is the current screen density and px is the value in the base (1x) density.

Or the other way around.

Also, remember to read the Best Practices from the Android Developers page!


If you still end up using px in your dimensions file which are not recommended, please take a look at this answer to know how to create different values folder for different screens.

Community
  • 1
  • 1
anthonymonori
  • 1,754
  • 14
  • 36
  • i dint understand i just want to know if i have puted 50dp in xhdpi then what value we have to put ldpi,mdpi, xxhdpi and xxxhdpi @anthonymonori – Manoj Kumar Nov 13 '17 at 09:26
  • @ManojKumar please read the first line after the quote on the top of my answer: "If you want to know what's 56dp in xhdpi, that's 56dp in ldpi, mdpi, hdpi, xxhdpi, xxxhdpi as well - as the definition says, it's density-independent pixels." – anthonymonori Nov 13 '17 at 09:40