DEV Community

Cover image for Show/hide  android soft keyboard with kotlin - 31 seconds of code.
Collins Kesuibai
Collins Kesuibai

Posted on • Updated on

Show/hide android soft keyboard with kotlin - 31 seconds of code.

What you will need and use.
  • InputMethodManager - The InputMethodManager is a client side API that exists in each application context and manages interactions.
  • View - Currently focused view.
  • Context
  • windowToken -Get windowToken from the window containing your currently focused view.
Showing the keyboard ⬆
private fun View.showKeyboard() {
        val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
    }

Enter fullscreen mode Exit fullscreen mode
Hiding the keyboard ⬇
private fun View.hideKeyboard() {
        val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(windowToken, 0)
    }
Enter fullscreen mode Exit fullscreen mode
Have a question don't hesitate to ask.Have a good day😊or night.hideKeyboard()// end of post ,share away.

Top comments (0)