Created
March 17, 2015 17:26
-
-
Save natuanorg/850a9290b4f18e9424fd to your computer and use it in GitHub Desktop.
Hide keyboard on touch outside of textfield
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
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