Skip to content

Instantly share code, notes, and snippets.

@xvadsan
Created December 23, 2020 11:20
Show Gist options
  • Save xvadsan/04ed19eba0444613816a02a28da1de8e to your computer and use it in GitHub Desktop.
Save xvadsan/04ed19eba0444613816a02a28da1de8e to your computer and use it in GitHub Desktop.
keyboard listener
private var mLastContentHeight by Delegates.notNull<Int>()
private val keyboardLayoutListener = OnGlobalLayoutListener {
val currentContentHeight: Int = requireActivity().window.findViewById<View>(Window.ID_ANDROID_CONTENT).getHeight()
if (mLastContentHeight > currentContentHeight + 100) {
showKeyboard()
mLastContentHeight = currentContentHeight
} else if (currentContentHeight > mLastContentHeight + 100) {
hideKeyboard()
mLastContentHeight = currentContentHeight
}
}
private fun initKeyboardListener() {
mLastContentHeight = requireActivity().window.findViewById<View>(Window.ID_ANDROID_CONTENT).height
requireActivity().getRootView().viewTreeObserver.addOnGlobalLayoutListener(keyboardLayoutListener)
}
requireActivity().getRootView().viewTreeObserver.removeOnGlobalLayoutListener(keyboardLayoutListener)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment