i want to hide the keyboard but i want to write it in a class. To use it for all activities. ı need a edit text delete focus code
class Extensions(){
fun hideSoftKeyboard(view: View) {
val imm =getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
}
problem description
No value passed for parameter 'serviceClass'
Type mismatch: inferred type is String but Context was expected
new code: but I couldn't do the outer click event
fun Activity.hideKeyboard() {
val view = currentFocus
if (view != null) {
view.clearFocus()
val inputMethodManager =
getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(
view.windowToken,
InputMethodManager.HIDE_NOT_ALWAYS
)
}
}
Extensions.kt
fun Activity.hideKeyboard(event: MotionEvent) {
if (event.action == MotionEvent.ACTION_DOWN){
val view = currentFocus
if (view != null) {
view.clearFocus()
val outRect = Rect()
view.getGlobalVisibleRect(outRect)
if (!outRect.contains(event.rawX.toInt(), event.rawY.toInt())) {
val inputMethodManager =
getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(
view.windowToken,
InputMethodManager.HIDE_NOT_ALWAYS
)
}
}
}
}
YouActivity
override fun dispatchTouchEvent(event: MotionEvent): Boolean {
hideKeyboard(event)
return super.dispatchTouchEvent(event)
}