For kotlin this is what worked for me.
private fun hideKeyboard() {
val inputManager = activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
if (inputManager.isAcceptingText) {
inputManager.hideSoftInputFromWindow(activity?.currentFocus?.windowToken, 0)
}
}