Android added notch support on API 28, but how to handle it on devices running API 27 (Honor 10, Huawei P20, etc.) ?
I was trying to use DisplayCutoutCompat
but I was not able to create an instance of it since documentation does not really point out how create one.
How to create the constructor parameter values: Rect safeInsets
, List<Rect> boundingRects
?
I also looked into the source code of the constructor, which is a bit confusing to me:
public DisplayCutoutCompat(Rect safeInsets, List<Rect> boundingRects) {
this(SDK_INT >= 28 ? new DisplayCutout(safeInsets, boundingRects) : null);
}
This will always return null on devices running API < 28. Thank you in advance.
Google provided notch related APIs in Android P. Devices with notch and API version lower than P implemented their own notch APIs.You can consult the APIs from device specified documentation.
Also I did not see creation of DisplayCutoutCompat instance in official documentation, but you can create DisplayCutout as follow:
I had similar issue, and had to use reflection to access what I need. My problem was that I had some calculations depending on screen size and while not accessing the notch space, the calculations were wrong and code didn't work well.
So you want to handle notch(display cutout) in android API lower than 28. That's horrible because different manufactures has different implementations. Nevertheless, all use Java reflection to get notch information. Factory design pattern should be used here.
Huawei display cutout
private static class HuaweiCutout implements ICutout {
}
Oppo display cutout
}
Vivo display cutout
}
Xiaomi display cutout of Android Oreo, of Android Pie
}
In case some manufactures' not coming up with a getNotchHeight() method, you can just use the status bar's height. Android has guarantee that notch height is at most the status bar height.
For Android Pie and above (
Build.VERSION.SDK_INT >= Build.VERSION_CODES.P
), you can use system's API to get notch information. Note the window must be attachedActivity#onAttachedToWindow
or you will get null DisplayCutout.