3

This is how I am getting the screen density using Android Studios

float density = getResources().getDisplayMetrics().density;

According to device specification, S8+ has 4.0 Density value and falls under xxxhdpi category

Refer this site https://material.io/devices/, Have Attached a screenshot for reference here

But the value returned by the above code is 2.9, which seems very wrong check here

Tried using densityDpi also, but it also returns xxhdpi as opposed to xxxhdpi

int densityDpi = context.getResources().getDisplayMetrics().densityDpi;

The above code works well on other devices, tested on OnePlus 5T, Nexus 6P, Redmi Note 4, Moto g4+ and others...facing issues with the Galaxy s8+, haven't tested for Galaxy s8 but i guess the result will be same.

Is this a know Bug, or am I doing something wrong?

Its getting difficult to manage layout for S8+ devices without the correct pixel density info for s8+ devices.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
GauRavK
  • 31
  • 1
  • 4

2 Answers2

0

You need to initialize display metrics object

You can try this:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
flat density=metrics.density;
flat densityDpi=metrics.densityDpi;
Paras Santoki
  • 821
  • 11
  • 26
0

Just like Surfman said. Your phone isn't at the maximum possible resolution. If you take the resolution reported within your screenshot and divide it by the reported 2.9 and multiply it by 4 you end up with roughly the maximum resolution of 2960x1440

LeDon
  • 529
  • 7
  • 21