Last active
March 20, 2023 04:08
-
-
Save akshaykalola28/ca07dc63aaf6e64f22d4e971ade068e3 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