/**
* Fires events when keyboard visibility changes
*/
fun Activity.onKeyboardVisibilityChange(action: (visible: Boolean) -> Unit) {
findViewById<View>(Window.ID_ANDROID_CONTENT)?.apply {
viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
val rectangle = Rect(0, 0, 0, 0)
val heightThreshold = resources.displayMetrics.density * KEYBOARD_VISIBILITY_THRESHOLD_DP
var wasOpen = false
override fun onGlobalLayout() {
val activityRoot = findViewById<ViewGroup>(Window.ID_ANDROID_CONTENT).getChildAt(0)
activityRoot.getWindowVisibleDisplayFrame(rectangle)
val heightDiff = activityRoot.rootView.height - rectangle.height()
val isOpen = heightDiff > heightThreshold
if (isOpen == wasOpen) return
wasOpen = isOpen
action.invoke(isOpen)
}
})
}
}
Created
May 1, 2020 10:05
-
-
Save NielsMasdorp/1a0a47fbd03ab1a074fdfe3e0f1428e9 to your computer and use it in GitHub Desktop.
Keyboard visibility events
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment