Skip to content

Instantly share code, notes, and snippets.

@e4basil
Forked from akshaykalola28/KeyboardVisibility.kt
Created March 20, 2023 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save e4basil/2a5a5b269fbff28957e4591d591f86fa to your computer and use it in GitHub Desktop.
Save e4basil/2a5a5b269fbff28957e4591d591f86fa 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