Skip to content

Instantly share code, notes, and snippets.

@natuanorg
Created March 17, 2015 17:26
Show Gist options
  • Save natuanorg/850a9290b4f18e9424fd to your computer and use it in GitHub Desktop.
Save natuanorg/850a9290b4f18e9424fd to your computer and use it in GitHub Desktop.
Hide keyboard on touch outside of textfield
private Rect mRect = new Rect();
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
final int action = MotionEventCompat.getActionMasked(ev);
int[] location = new int[2];
edtSearch.getLocationOnScreen(location);
mRect.left = location[0];
mRect.top = location[1];
mRect.right = location[0] + edtSearch.getWidth();
mRect.bottom = location[1] + edtSearch.getHeight();
int x = (int) ev.getX();
int y = (int) ev.getY();
if (action == MotionEvent.ACTION_DOWN && !mRect.contains(x, y)) {
InputMethodManager input = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
input.hideSoftInputFromWindow(edtSearch.getWindowToken(), 0);
}
return super.dispatchTouchEvent(ev);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment