I used the method below(in Kotlin):
*It will only work in Android P and above.
*My activity is a full screen activity.
*My assumption is android phone with notches are relatively new will either come out of the box with Android P or have been rapidly updated to Android P.
Create a OnGlobalLayoutListener
private val onDisplayCutoutReadyListener : ViewTreeObserver.OnGlobalLayoutListener = ViewTreeObserver.OnGlobalLayoutListener{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
var displayCutout = window.decorView.rootWindowInsets.displayCutout
if (displayCutout.boundingRects.size>0) {
val notchRect = displayCutout.boundingRects.get(0)
Log.v(TAG,"notch height in pixels="+notchRect.height())
}
}
}
Add the listener to your activity's rootview using .viewTreeObserver.addOnGlobalLayoutListener()
layout_fullscreen.viewTreeObserver.addOnGlobalLayoutListener(onDisplayCutoutReadyListener)
My layout xml where layout_fullscreen is the rootview.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_fullscreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
<com.baidu.mapapi.map.MapView
android:id="@+id/map_location"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>