LunarWatcher LunarWatcher - 2 months ago 17
Android Question

Getting dimensions of an Android device in dp

Samsung S6 Edge has the screen dimensions 1440x2560 pixels.

But that does not help me, as I need it in dp. How can I programatically find the dp dimensions(width and height) of an Android device?

Answer Source

Per the Supporting Multiple Screens guide's definition of dp

px = dp * (dpi / 160)

Therefore

dp = px / (dpi / 160)

or equivalently

dp = px * 160 / dpi

Remember that dp stands for 'density-independent pixel' - i.e., 1dp is the same physical size on a ldpi device as it is on an xxhdpi device. Therefore you should expect all phones to have roughly ~300-400dp of width, noting the bucket sizes by dp:

  • xlarge screens are at least 960dp x 720dp
  • large screens are at least 640dp x 480dp
  • normal screens are at least 470dp x 320dp
  • small screens are at least 426dp x 320dp

Credits @ianhanniballake