Skip to content

Instantly share code, notes, and snippets.

@Ahmad-Hamwi
Created July 28, 2021 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ahmad-Hamwi/3591a9fbb3fd656ed42accffce10edda to your computer and use it in GitHub Desktop.
Save Ahmad-Hamwi/3591a9fbb3fd656ed42accffce10edda to your computer and use it in GitHub Desktop.
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