Skip to content

Instantly share code, notes, and snippets.

@waseefakhtar
Last active March 14, 2020 17:04
@SuppressLint("NewApi")
private fun adjustToolbarMarginForNotch() {
// Notch is only supported by >= Android 9
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
val windowInsets = window.decorView.rootWindowInsets
if (windowInsets != null) {
val displayCutout = windowInsets.displayCutout
if (displayCutout != null) {
val safeInsetTop = displayCutout.safeInsetTop
val newLayoutParams = toolbar.layoutParams as ViewGroup.MarginLayoutParams
newLayoutParams.setMargins(0, safeInsetTop, 0, 0)
toolbar.layoutParams = newLayoutParams
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment