Skip to content

Instantly share code, notes, and snippets.

@ampatron
Last active August 29, 2015 14:04
Show Gist options
  • Save ampatron/38b9fea3fbd15f047b78 to your computer and use it in GitHub Desktop.
Save ampatron/38b9fea3fbd15f047b78 to your computer and use it in GitHub Desktop.
This hides soft keyboard after clicking outside EditText
//1. Make the parent view(content view of your activity) clickable and focusable by adding the following attributes
android:clickable="true"
android:focusableInTouchMode="true"
//2. Implement a hideKeyboard() method
public void hideKeyboard(View view) {
InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
//3. set the onFocusChangeListener of your edittext.
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(e);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment