Skip to content

Instantly share code, notes, and snippets.

@MilosSimic
Forked from elqsar/HideSoftkeyboardOnTap
Created December 25, 2017 16:48
Show Gist options
  • Save MilosSimic/30caaf2cfef5868f76f0f418c5d654e3 to your computer and use it in GitHub Desktop.
Save MilosSimic/30caaf2cfef5868f76f0f418c5d654e3 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