0

I'm making a game for Android using Java, I need to map the game screen dimensions to the device screen dimensions, I used to use the following code to get the device screen dimensions

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;

I face a problem with new devices with a notch, the above code gives me different screen height than the actual device, for example, the actual device height is 2340 pixels but the code returned 2130 pixels, I used the following code to get the notch height

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
    DisplayCutout displayCutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
    if (displayCutout.getBoundingRects().size() > 0) {
        Rect notchRect = displayCutout.getBoundingRects().get(0);
    }
}

which is 80, now 2130 + 80 is 2210, there are still 130 pixels missing when I map the game screen to the device screen, a shift happens due to the incorrect height

Udara Abeythilake
  • 1,215
  • 1
  • 20
  • 31
  • are you testing it on a device with Android P? – sanjeev May 31 '19 at 05:06
  • https://stackoverflow.com/a/53593067/6514945 – sanjeev May 31 '19 at 05:11
  • @sanjeev yes I'm testing on a device with Android P, please note that I know how to detect if the device has a notch, and I know how to get the notch dimensions, my problem is how to get the screen height in pixels, because the ordinary method returns wrong height – Eihab Ahmad May 31 '19 at 16:42
  • Check the answer properly. He checks for status bar height if it's more than 24dp while you are using the Android method to determine it. – sanjeev Jun 01 '19 at 19:54
  • And then? I need to get the total screen height in pixels – Eihab Ahmad Jun 01 '19 at 21:27

2 Answers2

0

Until now there is no way to get the real dimensions of the screen with a notch

0

Have you tried getRealMetrics instead of getMetrics?

user20191130
  • 340
  • 1
  • 9