A mobile phone screen is mainly divided into: the top status bar, the bottom virtual navigation bar and the middle application area.
Get the width and height of the entire screen
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
DisplayMetrics dm = getResources().getDisplayMetrics();
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;
Get the height of the status bar
public int getStatusBarHeight() {
int statusBarHeight = 0;
try {
Resources resources = getApplicationContext().getResources();
int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
statusBarHeight = resources.getDimensionPixelSize(resourceId);
} catch (Resources.NotFoundException e) {
e.printStackTrace();
}
return statusBarHeight;
}
Get the height of the bottom navigation bar
public int getNavigationBarHeight() {
int navigationBarHeight = 0;
try {
Resources resources = getApplicationContext().getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
navigationBarHeight = resources.getDimensionPixelSize(resourceId);
} catch (Resources.NotFoundException e) {
e.printStackTrace();
}
return navigationBarHeight;
}