I guess I have successfully implement equivalent methods (improving @RyanM's) to deprecated one.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Deprecated older method for comparison.
DisplayMetrics outMetrics = new DisplayMetrics();
getDisplay().getMetrics(outMetrics);
Log.d("Upto API-29", String.format(
"(width, height) = (%d, %d)", outMetrics.widthPixels, outMetrics.heightPixels
));
// Newer methods.
Log.d("API-30+", String.format(
"(width, height) = (%d, %d)", getScreenWidth(this), getScreenHeight(this)
));
}
public static int getScreenWidth(@NonNull Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowMetrics windowMetrics = activity.getWindowManager().getCurrentWindowMetrics();
Rect bounds = windowMetrics.getBounds();
Insets insets = windowMetrics.getWindowInsets().getInsetsIgnoringVisibility(
WindowInsets.Type.systemBars()
);
if (activity.getResources().getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE
&& activity.getResources().getConfiguration().smallestScreenWidthDp < 600
) { // landscape and phone
int navigationBarSize = insets.right + insets.left;
return bounds.width() - navigationBarSize;
} else { // portrait or tablet
return bounds.width();
}
} else {
DisplayMetrics outMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.widthPixels;
}
}
public static int getScreenHeight(@NonNull Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowMetrics windowMetrics = activity.getWindowManager().getCurrentWindowMetrics();
Rect bounds = windowMetrics.getBounds();
Insets insets = windowMetrics.getWindowInsets().getInsetsIgnoringVisibility(
WindowInsets.Type.systemBars()
);
if (activity.getResources().getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE
&& activity.getResources().getConfiguration().smallestScreenWidthDp < 600
) { // landscape and phone
return bounds.height();
} else { // portrait or tablet
int navigationBarSize = insets.bottom;
return bounds.height() - navigationBarSize;
}
} else {
DisplayMetrics outMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.heightPixels;
}
}
The points are
- SystemBar's height is not included in Window bound's height. We should exclude NavigationBar's height only (unless it is in landscape mode on phone device).
- In landscape mode on phone device, we should exclude NavigationBar's size from Window bound's width.
TESTS
1. On my real phone (API-30)
portrait:
2021-12-14 22:17:28.231 31660-31660/com.stackoverflow.windowmetrics D/Upto API-29: (width, height) = (1080, 2016)
2021-12-14 22:17:28.237 31660-31660/com.stackoverflow.windowmetrics D/API-30+: (width, height) = (1080, 2016)
landscape:
2021-12-14 22:17:35.858 31660-31660/com.stackoverflow.windowmetrics D/Upto API-29: (width, height) = (2016, 1080)
2021-12-14 22:17:35.887 31660-31660/com.stackoverflow.windowmetrics D/API-30+: (width, height) = (2016, 1080)
2. On emulated Nexus10 (API-31)
portrait:
2021-12-14 22:19:33.379 1416-1416/com.stackoverflow.windowmetrics D/Upto API-29: (width, height) = (1600, 2464)
2021-12-14 22:19:33.382 1416-1416/com.stackoverflow.windowmetrics D/API-30+: (width, height) = (1600, 2464)
lanscape:
2021-12-14 22:18:44.809 1416-1416/com.stackoverflow.windowmetrics D/Upto API-29: (width, height) = (2560, 1504)
2021-12-14 22:18:44.814 1416-1416/com.stackoverflow.windowmetrics D/API-30+: (width, height) = (2560, 1504)
2. On emulated Nexus7 (API-31)
portrait:
2021-12-14 22:21:21.606 3108-3108/com.stackoverflow.windowmetrics D/Upto API-29: (width, height) = (800, 1216)
2021-12-14 22:21:21.610 3108-3108/com.stackoverflow.windowmetrics D/API-30+: (width, height) = (800, 1216)
landscape:
2021-12-14 22:22:23.283 3108-3108/com.stackoverflow.windowmetrics D/Upto API-29: (width, height) = (1280, 736)
2021-12-14 22:22:23.289 3108-3108/com.stackoverflow.windowmetrics D/API-30+: (width, height) = (1280, 736)