Skip to content

Instantly share code, notes, and snippets.

@gajerarajnit
Created August 22, 2017 06:10

Revisions

  1. gajerarajnit created this gist Aug 22, 2017.
    13 changes: 13 additions & 0 deletions hide_keyboard.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
    View view = getCurrentFocus();
    if (view != null && (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_MOVE) && view instanceof EditText && !view.getClass().getName().startsWith("android.webkit.")) {
    int scrcoords[] = new int[2];
    view.getLocationOnScreen(scrcoords);
    float x = ev.getRawX() + view.getLeft() - scrcoords[0];
    float y = ev.getRawY() + view.getTop() - scrcoords[1];
    if (x < view.getLeft() || x > view.getRight() || y < view.getTop() || y > view.getBottom())
    ((InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow((this.getWindow().getDecorView().getApplicationWindowToken()), 0);
    }
    return super.dispatchTouchEvent(ev);
    }