Here you have two static functions to hide keyboard, depend on your case which one you want to use. I tested on Android Oreo and it works.
object UIHelper {
fun hideSoftKeyboard(activity: Activity?) {
if (activity != null) {
val inputManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
if (activity.currentFocus != null && inputManager != null) {
inputManager.hideSoftInputFromWindow(activity.currentFocus!!.windowToken, 0)
inputManager.hideSoftInputFromInputMethod(activity.currentFocus!!.windowToken, 0)
}
}
}
fun hideSoftKeyboard(view: View?) {
if (view != null) {
val inputManager = view!!.getContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManager?.hideSoftInputFromWindow(view!!.getWindowToken(), 0)
}
}
fun showKeyboard(activityContext: Context, editText: EditText) {
editText.requestFocus()
Handler().postDelayed(Runnable {
val inputMethodManager = activityContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT)
}, 250)
}
}
Example of use:
UIHelper.hideSoftKeyboard(this)
UIHelper.hideSoftKeyboard(passwordField)
To show:
UIHelper.showKeyboard(this, passwordField)