How to handle notch(display cutout) in android API lower than 28?

blade picture blade · Aug 8, 2018 · Viewed 12.1k times · Source

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.

Answer

fujino ryougi picture fujino ryougi · Aug 28, 2018

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:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            DisplayCutout displayCutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
}