1

The question is self explanatory.

Show soft keyboard when your edit text gains focus and hide keyboard when it loses focus. Here is the code that I have used.

this.newTaskTitle = (EditText) taskCreationView.findViewById(R.id.newTaskTitle);
    this.newTaskTitle.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            //Set up input manager
            InputMethodManager keyboardManager = (InputMethodManager) getSystemService(
                    Context.INPUT_METHOD_SERVICE
            );
            if(hasFocus) {
                Log.i(TAG,"hasFocus");
                //Display keyboard
                keyboardManager.showSoftInput(
                        v,
                        InputMethodManager.SHOW_IMPLICIT
                );
            } else {
                Log.i(TAG,"lostFocus");
               //Hide keyboard
                keyboardManager.hideSoftInputFromInputMethod(
                        v.getWindowToken(),
                        0
                );
            }
        }
    });

Even though the else executes when the EditText loses focus, the keyboard is never hidden. Why would that be ?

Isn't this the right way to hide the keyboard ?

mahesmohan
  • 686
  • 1
  • 11
  • 30
  • Do you next button or Done button on soft keyboard? – Fahad Ishaque Jan 28 '15 at 12:54
  • I'm not *very* sure, but maybe you need to typecast the view to an EditText first. EditText myEditText=(EditText)v; and then myEditText.getWindowToken() – vipluv Jan 28 '15 at 12:55
  • @FahadIshaque No, I have a button in my layout that removes the edittext by setting its visibility to GONE. OnClick of that button explicitly calls clearFocus on the edit text and requests focus on some other view. But the keyboard stays there. – mahesmohan Jan 28 '15 at 12:57
  • @vipluv that didn't work either.. – mahesmohan Jan 28 '15 at 13:01
  • @mahesmohan well, sorry. try some of the answers here: http://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard – vipluv Jan 28 '15 at 13:04

1 Answers1

1

I think, there is no need to set OnFocusChangeListener. Call below method from onClick of your button and after calling this method set visibility GONE of your EditText.On gaining focus soft keyboard get open automatically.

private  void hideKeyBoard(Context context, EditText editText) {
        InputMethodManager imm = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    }
Ali
  • 1,693
  • 18
  • 23
  • somehow placing the method as a member of the Activity did the trick. Thanks for the help Ali. But then again a question remains why didn't it work when the Input manager was initialized inside the onFocusChange of the EditText ? – mahesmohan Jan 28 '15 at 13:31
  • Actually, implementing OnFocusChangeListener is not right way to show and hide soft keyboard. perhaps you were setting visibility of EditText to gone and onFocusChange not get called at all or may be something else. – Ali Jan 28 '15 at 13:45
  • I did set visibility as GONE but that was only after explicitly calling clearFocus() on the EditText. The log also listed the "lostFocus" info message I added in the else part but the keyboard never hid. – mahesmohan Jan 28 '15 at 13:49