Skip to content

Instantly share code, notes, and snippets.

@akshaykalola28
Last active March 20, 2023 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save akshaykalola28/ca07dc63aaf6e64f22d4e971ade068e3 to your computer and use it in GitHub Desktop.
Save akshaykalola28/ca07dc63aaf6e64f22d4e971ade068e3 to your computer and use it in GitHub Desktop.
Keyboard visibility for android (activity and fragment)
import android.app.Activity
import android.view.View
import android.view.inputmethod.InputMethodManager
import androidx.fragment.app.Fragment
fun Activity.hideKeyboard() {
val imm: InputMethodManager =
getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
val view = currentFocus ?: View(this)
imm.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}
fun Fragment.hideKeyboard() {
activity?.apply {
val imm: InputMethodManager =
getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
val view = currentFocus ?: View(this)
imm.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment