I used SOFT_INPUT_ADJUST_RESIZE in order to show all content when keyboard pops up.
Following documentation, I added new code pieces:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
requireActivity().window.setDecorFitsSystemWindows(false)
}
and
binding.constraintLayoutRoot.setOnApplyWindowInsetsListener { _, windowInsets ->
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val insets = windowInsets.getInsets(WindowInsets.Type.ime() or WindowInsets.Type.systemGestures())
insets
}
windowInsets
}
For some reason, view does not resize based on the fact if the keyboard appears or not.
As per google developers from Android API 30, they have deprecated it and provided us with a workaround setDecorFitsSystemWindows. But this will not work below Android 30 for below 30 you have to use the same method as before.
How it works: When setDecorFitsSystemWindows is true the framework will check SYSTEM_UI_LAYOUT_FLAGS as well the WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE flag and fits content according to these flags.
But when it is set to False the framework will not fit the content view to the insets and will check your WindowInsets to set the content view.
First, you have to define your windowsInsets which it can use
binding.root.setOnApplyWindowInsetsListener { _, windowInsets ->
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val imeHeight = windowInsets.getInsets(WindowInsets.Type.ime()).bottom
binding.root.setPadding(0, 0, 0, imeHeight)
}
windowInsets
}
and then when you want your screen to use ADUST_RESIZE set false
setDecorFitsSystemWindows(false)
When you want back to normal set true
setDecorFitsSystemWindows(true)
Managing for API 30 above and below use conditions
var shouldResize = false // false will resize
binding.button.setOnClickListener {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.setDecorFitsSystemWindows(shouldResize)
shouldResize = shouldResize.not()
} else {
if (shouldResize.not()) {
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
} else {
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
}
}
}
Reference link:
Android SOFT_INPUT_ADJUST_RESIZE
Android setDecorFitsSystemWindows
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With