-
-
Save e4basil/2a5a5b269fbff28957e4591d591f86fa to your computer and use it in GitHub Desktop.
Keyboard visibility for android (activity and fragment)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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