Skip to content

Instantly share code, notes, and snippets.

@tizisdeepan
Last active April 22, 2019 06:45
Show Gist options
  • Save tizisdeepan/6d14872d876e3be1f42e93a11e601fe2 to your computer and use it in GitHub Desktop.
Save tizisdeepan/6d14872d876e3be1f42e93a11e601fe2 to your computer and use it in GitHub Desktop.
A class to hide the keypad that has been opened in any screen. It's simple to use and does the job.
import android.app.Activity
import android.content.Context
import androidx.fragment.app.FragmentActivity
import android.view.inputmethod.InputMethodManager
class HideKeypad {
fun now(activity: Any?) {
if (activity != null) {
if (activity is Activity) {
val inputManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
val currentFocusedView = activity.currentFocus
if (currentFocusedView != null) {
inputManager.hideSoftInputFromWindow(currentFocusedView.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}
} else if (activity is FragmentActivity) {
val inputManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
val currentFocusedView = activity.currentFocus
if (currentFocusedView != null) {
inputManager.hideSoftInputFromWindow(currentFocusedView.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}
}
}
}
}
@tizisdeepan
Copy link
Author

tizisdeepan commented Apr 22, 2019

USAGE

HideKeypad.now(Activity Reference)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment