Do we still have no proper keyboard show/hide even...
# android
r
Do we still have no proper keyboard show/hide event handling on Android in 2019? /rant
😁 1
👍 3
stackoverflow 3
Anyone got a better way of doing it than this?
Copy code
inline fun View.onKeyboardStateChanged(crossinline action: (visible: Boolean) -> Unit) {
    viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {

        private val estimatedSoftKeyboardHeight = 100
        private val density = resources.displayMetrics.density
        private val visibleRect = Rect()
        private var isKeyboardShown = false

        override fun onGlobalLayout() {
            if (isKeyboardShown() != isKeyboardShown) {
                isKeyboardShown = !isKeyboardShown
                action(isKeyboardShown)
            }
        }

        private fun isKeyboardShown(): Boolean {
            rootView.getWindowVisibleDisplayFrame(visibleRect)
            return rootView.bottom - visibleRect.bottom > estimatedSoftKeyboardHeight * density
        }

    })
}
d
That looks like how I had to handle that problem almost five years ago 😞
l
Maybe at Google I/O they will uncover a solution for that (they talked about their intentions to make this easier), but for now, that's about all we got.
a
i have heard that Google gona fix this in 2019 🙂 Looking forward to see solution at I/O
Copy code
fun Activity.openKeyboard() {
    try {
        getSystemService<InputMethodManager>()?.showSoftInput(currentFocus, SHOW_IMPLICIT)
    } catch (e: Exception) {
    }
}

fun Activity.closeKeyboard() {
    try {
        getSystemService<InputMethodManager>()?.hideSoftInputFromWindow(
            currentFocus?.windowToken, HIDE_NOT_ALWAYS
        )
    } catch (e: Exception) {
    }
}
r
Well I do know how to open and close the keyboard, that’s not what’s missing in Android 🤷‍♂️