Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A useful binding adapter that will update the view's padding according to the system bar insets.
/**
* Example:
*
* <!--parent container (that's expanded to fullscreen)-->
* <androidx.constraintlayout.widget.ConstraintLayout
* android:layout_width="match_parent"
* android:layout_height="match_parent"
* ...
* app:applySystemBarsInsetOnPadding="@{true}">
* ...
* ...
* ...
* </androidx.constraintlayout.widget.ConstraintLayout>
*/
@BindingAdapter("app:applySystemBarsInsetOnPadding")
fun applySystemBarsInsetOnPadding(view: View, apply: Boolean) {
if (!apply) return
ViewCompat.setOnApplyWindowInsetsListener(view) { _, windowInsets ->
val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
view.updatePadding(bottom = insets.bottom, top = insets.top)
windowInsets
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment