Skip to content

Instantly share code, notes, and snippets.

@pokk
Last active November 8, 2016 08:34
Show Gist options
  • Save pokk/e975d40def108a0e67a29c065f394f57 to your computer and use it in GitHub Desktop.
Save pokk/e975d40def108a0e67a29c065f394f57 to your computer and use it in GitHub Desktop.
Hide soft keyboard when clicking the outside of EditText

Introduction

When touch out of the edit text's range, the soft keyboard will be closed automatically in Android.

/**
* When touch out of the edit text's range, the soft keyboard will be closed automatically.
*/
@Override
public boolean onTouchEvent(MotionEvent event)
{
InputMethodManager manager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
if(getCurrentFocus()!= null && getCurrentFocus().getWindowToken()!= null)
{
manager.hideSoftInputFromWindow(getCurrentFocus().InputMethodManager.HIDE_NOT_ALWAYS);
}
}
return super.onTouchEvent(event);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment