Skip to content

Instantly share code, notes, and snippets.

@luongvo
Forked from elqsar/HideSoftkeyboardOnTap
Created May 21, 2017 18:26
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 luongvo/d0a06b8549c85f581f9091c6fdb9bbd4 to your computer and use it in GitHub Desktop.
Save luongvo/d0a06b8549c85f581f9091c6fdb9bbd4 to your computer and use it in GitHub Desktop.
Hide softkeyboard in android application if user tap outside EditText control
protected void setupParent(View view) {
//Set up touch listener for non-text box views to hide keyboard.
if(!(view instanceof EditText)) {
view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
hideSoftKeyboard();
return false;
}
});
}
//If a layout container, iterate over children
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
setupParent(innerView);
}
}
}
private void hideSoftKeyboard() {
InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment