Skip to content

Instantly share code, notes, and snippets.

Avatar

Fernando García Álvarez fercarcedo

View GitHub Profile
View controlinsetsanimation.kt
window.insetsController?.controlWindowInsetsAnimation(
WindowInsets.Type.ime(),
-1,
null,
listener
)
View styles.xml
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Rest of the theme attributes... -->
<item name="android:navigationBarColor">@android:color/transparent</item>
</style>
View showkeyboard.kt
// 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())
}
View newfullscreen.kt
// New way of requesting the app to be laid out fullscreen
// (for example, when implementing edge-to-edge)
window.setDecorFitsSystemWindows(false)
View systemuivisibility.kt
// 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 insetstypes.kt
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
}
View controlime.kt
scrollView.viewTreeObserver.addOnScrollChangedListener {
animationController?.setInsetsAndAlpha(Insets.of(0, 0, 0, scrollView.scrollY), 1f, 0f)
}
View insetscontrollistener.kt
val listener = object : WindowInsetsAnimationControlListener {
override fun onCancelled() {
animationController = null
}
override fun onReady(controller: WindowInsetsAnimationController, types: Int) {
animationController = controller
}
}
View synchronizeime.kt
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
}
}
View imeinsets.kt
val imeInsets = view.rootWindowInsets.getInsets(WindowInsets.Type.ime())
if (imeInsets.isVisible()) {
// Move view by the height of the IME
view.translationX = imeInsets.bottom
}