ribesg
03/19/2019, 3:50 PMribesg
03/19/2019, 4:06 PMinline 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
}
})
}
dalexander
03/19/2019, 4:48 PMlouiscad
03/19/2019, 11:49 PMahulyk
03/20/2019, 9:38 AMahulyk
03/20/2019, 9:39 AMfun 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) {
}
}
ribesg
03/20/2019, 9:48 AM