Skip to content

Instantly share code, notes, and snippets.

View fercarcedo's full-sized avatar

Fernando García Álvarez fercarcedo

View GitHub Profile
window.insetsController?.controlWindowInsetsAnimation(
WindowInsets.Type.ime(),
-1,
null,
listener
)
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Rest of the theme attributes... -->
<item name="android:navigationBarColor">@android:color/transparent</item>
</style>
// You have to wait for the view to be attached to the
// window (otherwise, windowInsetController will be null)
view.doOnLayout {
view.windowInsetsController?.show(WindowInsets.Type.ime())
// You can also access it from Window
window.insetsController?.show(WindowInsets.Type.ime())
}
// New way of requesting the app to be laid out fullscreen
// (for example, when implementing edge-to-edge)
window.setDecorFitsSystemWindows(false)
// Old way (now deprecated) of requesting the app to be
// laid out fullscreen and hiding the navigation
view.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
view.setOnApplyWindowInsetsListener { view, windowInsets ->
val systemWindowInsets = windowInsets.getInsets(WindowInsets.Type.systemBars())
// It's also possible to use multiple types
val insets = windowInsets.getInsets(
WindowInsets.Type.ime() or
WindowInsets.Type.systemGestures()
)
windowInsets
}
scrollView.viewTreeObserver.addOnScrollChangedListener {
animationController?.setInsetsAndAlpha(Insets.of(0, 0, 0, scrollView.scrollY), 1f, 0f)
}
val listener = object : WindowInsetsAnimationControlListener {
override fun onCancelled() {
animationController = null
}
override fun onReady(controller: WindowInsetsAnimationController, types: Int) {
animationController = controller
}
}
val inputLayoutMarginBottom = inputLayout.marginBottom
val callback = object : WindowInsetsAnimation.Callback(DISPATCH_MODE_STOP) {
override fun onProgress(insets: WindowInsets, animations: MutableList<WindowInsetsAnimation>): WindowInsets {
inputLayout.updateLayoutParams<ViewGroup.MarginLayoutParams> {
updateMargins(bottom = inputLayoutMarginBottom +
insets.getInsets(WindowInsets.Type.ime()).bottom)
}
return insets
}
}
val imeInsets = view.rootWindowInsets.getInsets(WindowInsets.Type.ime())
if (imeInsets.isVisible()) {
// Move view by the height of the IME
view.translationX = imeInsets.bottom
}