When I click on TextField, I need to scroll UI upwards to show login button to the user and not hide it behind keyboard.
I am using RelocationRequester for the same.
I am using this for detecting keyboard show/hide event:
fun listenKeyboard() {
val activityRootView =
(requireActivity().findViewById<View>(android.R.id.content) as ViewGroup).getChildAt(0)
activityRootView.viewTreeObserver.addOnGlobalLayoutListener(object :
ViewTreeObserver.OnGlobalLayoutListener {
private var wasOpened = false
private val DefaultKeyboardDP = 100
private val EstimatedKeyboardDP =
DefaultKeyboardDP + 48
private val r: Rect = Rect()
override fun onGlobalLayout() {
val estimatedKeyboardHeight = TypedValue
.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
EstimatedKeyboardDP.toFloat(),
activityRootView.resources.displayMetrics
)
.toInt()
activityRootView.getWindowVisibleDisplayFrame(r)
val heightDiff: Int = activityRootView.rootView.height - (r.bottom - r.top)
val isShown = heightDiff >= estimatedKeyboardHeight
if (isShown == wasOpened) {
return
}
wasOpened = isShown
keyboardVisibleState(isShown)
}
})
}
and once the keyboard is visible, I am calling the relocationRequestor's bringIntoView().
coroutineScope.launch {
delay(250)
relocationRequester.bringIntoView()
}
Its behaving randomly, working on some devices and not on others. Is there any better solution to deal with this issue?
WindowInsets.isImeVisible
– Dichotomous