I am using to get the device with and height using of the following code.
screenHeight = getWindowManager()
.getDefaultDisplay().getHeight();
screenWidth = getWindowManager()
.getDefaultDisplay().getWidth();
But getDefaultDisplay() is deprecated. So I search for the solution and I got the following solution.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
final WindowMetrics metrics = getWindowManager().getCurrentWindowMetrics();
// Gets all excluding insets
final WindowInsets windowInsets = metrics.getWindowInsets();
Insets insets = windowInsets.getInsetsIgnoringVisibility(WindowInsets.Type.navigationBars()
| WindowInsets.Type.displayCutout());
screenWidth = insets.right + insets.left;
screenHeight = insets.top + insets.bottom;
}else{
screenHeight = getWindowManager()
.getDefaultDisplay().getHeight();
screenWidth = getWindowManager()
.getDefaultDisplay().getWidth();
}
But Here I am getting always zero for the width and height 126. Please help me on this.
I tried the following code also. But not getting the exact screen width and height.
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
float dpHeight = displayMetrics.heightPixels / displayMetrics.density;
float dpWidth = displayMetrics.widthPixels / displayMetrics.density;
screenHeight = convertDpToPixel(dpHeight,this);
screenWidth = convertDpToPixel(dpWidth,this);