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