Last active
April 22, 2019 06:45
-
-
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.
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.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) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
USAGE
HideKeypad.now(Activity Reference)