From API 29 or Android 10, Android introduced a feature for Display cutouts on the screen (for camera for example).
These are call Insets and can be accessed via DisplayCutout object. In this example is described how to get the size of this cutouts:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
private fun cutoutDimensions() { // Calculate cutout for devices that supports that feature (notch on top) var cutOutTopHeight = 0 var cutOutTopWidth = 0 var cutOutBottom = 0 var cutOutLeft = 0 var cutOutRight = 0 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { if (window.decorView.rootWindowInsets != null) { val displayCutout = window.decorView.rootWindowInsets.displayCutout if (displayCutout != null) { cutOutTopHeight = displayCutout.boundingRectTop.height() cutOutTopWidth = displayCutout.boundingRectTop.width() cutOutBottom = displayCutout.boundingRectBottom.height() cutOutLeft = displayCutout.boundingRectLeft.height() cutOutRight = displayCutout.boundingRectRight.height() } } } println("CUTOUT---> Top height: $cutOutTopHeight width: $cutOutTopWidth Bottom: $cutOutBottom Left: $cutOutLeft Right: $cutOutRight") } |
We must at least target API level 29 and above. Also keep in mind that if you want to get the values from DisplayCutout object, the root view (for activity or fragment) needs to be rendered (or lay out) on screen:
1 2 3 |
binding.rootContainer.doOnLayout { cutoutDimensions() } |