Created
October 17, 2016 11:20
-
-
Save androidcodehunter/3f7fb3c26ceb48730b6671194a9bfae1 to your computer and use it in GitHub Desktop.
Hide Softkeyboard outside clicking of Edittext.
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
@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