0

I think I'm unable to use the correct words to find my desired code for this as I think this is doable.

I simply want to close the keyboard view from android, if I press anywhere else than on the keys. (above the keyboard actually...)

E.g. when I'm in an EditView, done editting.

  • Here's what I was looking for: http://stackoverflow.com/questions/4165414/how-to-hide-soft-keyboard-on-android-after-clicking-outside-edittext –  Nov 07 '14 at 08:45

3 Answers3

0

This is the function you should use to hide soft keyboard

public static void hideSoftKeyboard(Context context){
    InputMethodManager inputMethodManager = (InputMethodManager) context
            .getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(context.getCurrentFocus()
            .getWindowToken(), 0);
}
Mukesh Rana
  • 4,051
  • 3
  • 27
  • 39
  • And where exactly should I call this method? –  Nov 07 '14 at 08:22
  • you have to apply some sort of touch listener(especially a singleTap) on a View(probably be the parent view) in your Activity on whom click you want to hide your keyboard.Show me your layout i'll tell you – Mukesh Rana Nov 07 '14 at 09:28
0
EditText myEditText = (EditText) findViewById(R.id.myEditText);  
InputMethodManager imm = (InputMethodManager)getSystemService(
      Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
Mina Fawzy
  • 20,852
  • 17
  • 133
  • 156
  • Seems not to be working for me. When I click the EditView and then click anywhere on the screen, the keyboard still stays there. –  Nov 07 '14 at 08:23
0
EditText myEditText = (EditText) findViewById(R.id.myEditText); 
myEditText.setOnEditorActionListener(onEditorActionListener);

protected OnEditorActionListener onEditorActionListener = new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            resetTimeout();
            InputMethodManager imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            v.clearFocus();
        }

        return false;
    }
};
Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106