Skip to content

Instantly share code, notes, and snippets.

@androidcodehunter
Created October 17, 2016 11:20
Show Gist options
  • Save androidcodehunter/3f7fb3c26ceb48730b6671194a9bfae1 to your computer and use it in GitHub Desktop.
Save androidcodehunter/3f7fb3c26ceb48730b6671194a9bfae1 to your computer and use it in GitHub Desktop.
Hide Softkeyboard outside clicking of Edittext.
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
View v = getCurrentFocus();
boolean result = super.dispatchTouchEvent(event);
if (v instanceof EditText) {
View w = getCurrentFocus();
int coords[] = new int[2];
w.getLocationOnScreen(coords);
float x = event.getRawX() + w.getLeft() - coords[0];
float y = event.getRawY() + w.getTop() - coords[1];
if (event.getAction() == MotionEvent.ACTION_UP && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom())) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment