1

I have read documentation, extract data from other sources as well, but unable to understand this very basic thing.

In android we use DP, because its responsive and it stretches resources with respect to the screen sizes.

My question is there are different densities of different screens. Lets consider following 2 screen densities.

  1. mdpi- 160dpi
  2. hdpi-240 dpi

Let say i define width-height of an image as 20dpX20dp People say don't use px, because it varies from device to device, but dpi also varying from device to device,

I know it works fine, but i want to understand the science behind this, how does it manages, when there are different dpi's of different screen.

According to my understanding, 1dpi of screen won't be equal to 1dpi of other screen.

Kindly guide me. I have just give up on this.

dev90
  • 7,187
  • 15
  • 80
  • 153
  • Possible duplicate of [Difference between px, dp, dip and sp on Android?](http://stackoverflow.com/questions/2025282/difference-between-px-dp-dip-and-sp-on-android) – David Medenjak May 13 '16 at 13:31

1 Answers1

2

There are two things to consider when considering the "size" of something on a digital screen: the size of the object in pixels and the physical size of the object.

When you specify the size of an object in pixels, it will have a different physical size on screens with different densities.

Take for example two devices with 5" screens. Device A has a 1920 x 1080 pixel display, while Device B has a 960 x 540 pixel display. Both are the same physical size, but have wildly different numbers of pixels on the screen. If you have an image that is 50 pixels wide by 50 pixels tall, that image will appear to be much smaller on Device A because the pixels on Device A are packed together much more closely.

DPs are a unit that allows us to specify a consistent size for objects across these varying devices. If you specify that an object should be 160dp wide, it will be about an inch wide on every device. On an mdpi device, that will be 160 pixels, but on an xhdpi device that will be 320 pixels.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • Thanks, So it means `20dp` for each screen is different. On `mdpi` `20dp` has different value, and on `xxdpi` 20dp has some other value. – dev90 May 13 '16 at 13:45
  • let say i define `20dp`, `width-height` of an image, then how would Android know that this `20dp` is for which screen. – dev90 May 13 '16 at 13:48
  • 2
    20dp is always 20dp no matter what device your app is running on. Android converts this to a pixel value based on the density of your device. MDPI is the baseline, meaning that 20dp on an mdpi device is 20 pixels. HDPI is scaled at 1.5x, so 20dp is 30px. XHDPI is scaled at 2x, so 20dp is 40 px. – Bryan Herbst May 13 '16 at 13:57
  • Thanks a lot, One last thing. Let say i have not added layouts explicitly, `large-layout`, `x-large-layout` etc..So does it mean the default i am using is the `m-dpi` layout? – dev90 May 13 '16 at 14:06
  • 1
    Typically there isn't a need for a `layout` folder with a density qualifier (like `layout-hdpi`). Instead, you typically define your sizes in your existing layouts using DPs, which Android will scale properly. You typically only provide image assets (like PNGs) in density-specific folders (e.g. `drawable-hdpi`) so that you can provide higher-quality images on higher-density devices. – Bryan Herbst May 13 '16 at 14:09
  • but the `22dp` `width-height` i defined in `layout file`is for which layout ? – dev90 May 13 '16 at 14:38
  • 1
    Whichever layout file you put it in. It will be applicable to all densities, unless you override it for a particular density. – Bryan Herbst May 13 '16 at 14:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/111877/discussion-between-kirmani88-and-tanis-7x). – dev90 May 13 '16 at 14:39
  • Thanks, sorry for making it long... What i am understanding from your point is that,if i simply write 22dpi , and its looking perfect in the layout preview screen, then if i open this layout in low-dpi screen or xx-dpi screen it will look same in size? – dev90 May 13 '16 at 14:49
  • 1
    Yes, it should appear to be the same size on all devices – Bryan Herbst May 13 '16 at 16:19
  • Thanks a lot for your time and answers !! :) – dev90 May 13 '16 at 16:31