Guest User

Untitled

a guest
May 5th, 2021
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.73 KB | None | 0 0
  1. fun Activity.hideKeyboard(event: MotionEvent) {
  2.     if (event.action == MotionEvent.ACTION_DOWN) {
  3.         val v = currentFocus
  4.         if (v is EditText) {
  5.             val outRect = Rect()
  6.             v.getGlobalVisibleRect(outRect)
  7.             if (!outRect.contains(event.rawX.toInt(), event.rawY.toInt())) {
  8.                 v.clearFocus()
  9.                 val imm: InputMethodManager =
  10.                     getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
  11.                 imm.hideSoftInputFromWindow(v.getWindowToken(), 0)
  12.             }
  13.         }
  14.     }
  15. }
  16.  
  17.  
  18.     override fun dispatchTouchEvent(event: MotionEvent): Boolean {
  19.         hideKeyboard(event)
  20.         return super.dispatchTouchEvent(event)
  21.     }
Advertisement
Add Comment
Please, Sign In to add comment