Created
July 28, 2021 18:11
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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