It can't work well for the Android P which contain the cutout mode.
Our suggestion is :
`
int orientation = getScreenOrientation();
int topCutoutHeight = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
topCutoutHeight = getTopCutoutHeight();
}
int keyboardHeight = screenSize.y + topCutoutHeight - rect.bottom;
@TargetApi(android.os.Build.VERSION_CODES.P)
private int getTopCutoutHeight(){
View decorView = activity.getWindow().getDecorView();
if(decorView == null){
return 0;
}
int cutOffHeight = 0;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
WindowInsets windowInsets = decorView.getRootWindowInsets();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
DisplayCutout displayCutout = windowInsets.getDisplayCutout();
if(displayCutout != null){
List<Rect> list = displayCutout.getBoundingRects();
for(Rect rect : list){
if(rect.top == 0){
cutOffHeight += rect.bottom - rect.top;
}
}
}
}
}
return cutOffHeight;
}
`
What about on apps targeting lower APIs? DisplayCutout is not an option. How do we handle this?
Our suggestion is : ` int orientation = getScreenOrientation(); int topCutoutHeight = 0; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { topCutoutHeight = getTopCutoutHeight(); } int keyboardHeight = screenSize.y + topCutoutHeight - rect.bottom;
@TargetApi(android.os.Build.VERSION_CODES.P) private int getTopCutoutHeight(){ View decorView = activity.getWindow().getDecorView(); if(decorView == null){ return 0; } int cutOffHeight = 0; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { WindowInsets windowInsets = decorView.getRootWindowInsets(); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) { DisplayCutout displayCutout = windowInsets.getDisplayCutout(); if(displayCutout != null){ List<Rect> list = displayCutout.getBoundingRects(); for(Rect rect : list){ if(rect.top == 0){ cutOffHeight += rect.bottom - rect.top; } } } } } return cutOffHeight; }`
What about on apps targeting lower APIs? DisplayCutout is not an option. How do we handle this?