I would like to close the soft keyboard from inside a fragment. I am finding a lot of answers but they are all in Java. Usually I am able to transition it to Kotlin quite easily but I am having a hard time with this one.
The closest I got was this:
fun closeKeyboard() {
val activity = activity as FeedActivity
val view = activity.currentFocus
if (view != null) {
val imm = ContextCompat.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
imm!!.hideSoftInputFromWindow(view!!.getWindowToken(), 0)
}
}
But I am having trouble with this part getSystemService(Context.INPUT_METHOD_SERVICE)
All is right except ContextCompat.getSystemService
. Use Activity
instance instead ContextCompat
.
val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view!!.getWindowToken(), 0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With