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