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
)
}
}
4
Answers
Extensions.kt
YouActivity
if you are using Jetpack Compose;
You can make an Extenstion function on the edit text that would do that for you
I haven’t tested it out yet, please test that one out and tell me if it does work.
// on below line getting current view.
val view: View? = this.currentFocus