Last active
August 29, 2015 14:04
-
-
Save ampatron/38b9fea3fbd15f047b78 to your computer and use it in GitHub Desktop.
This hides soft keyboard after clicking outside 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
//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