1

I'm building a simple Hangman app. I have currently set the keyBoard to hide after the user touches the submit button and restricted the EditText to a maxLength of "1".

What I want is for the Keyboard to hide right after the user touches a letter key and successfully inputs a valid entry into the EditText. I've tried implementing KeyEvent methods as used here and modified it to listen for any key code with the getMaxKeyCode() method.

UPDATE***: To clarify, I'm not having an issue with the ENTER button, that is working fine. I want the keyboard to hide after the user touches whichever letter they are guessing. So, if the user taps the 'E' key, the keyboard will hide as soon as the 'E' char appears in the EditText field.

Here's some of the code I'm using in the current iteration:

 @OnClick(R.id.guess_button)
void submit() {
    submitLetter();
}

private void submitLetter() {
    Editable userInput = mGuessInput.getText();
    String guessStr = mUserInput.toString();
    if (mGuessStr.length() != 0) {
        checkGuess(
                String.valueOf(mGuessStr).charAt(0),
                mCodeWord.toUpperCase()
        );
        mUserInput.clear();
        mGuessInput.clearFocus();
        hideKeyboard(MainActivity.this);
    }
}
public static void hideKeyboard(Activity activity) {
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
   View view = activity.getCurrentFocus();
    if (view == null) {
        view = new View(activity);
    }
    if (imm != null) {
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}

And here's what I'm trying to get to work:

public void hideKeyboardOnKeyTouch(EditText editText) {
    final int generatedKeyCode = KeyEvent.getMaxKeyCode();
    editText.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (event.getAction() == generatedKeyCode) {
             hideKeyboard(MainActivity.this);
            }
            return false;
        }
    });

}

Then I call the above method on mGuessInput (the EditText View) in onCreate(). I've also tried checking for the ACTION_UP event in this method, but still nothing.

There's probably a simple solution that I'm missing but I think I'm not seeing the forest for the trees. I hope this isn't a duplicate, but I can't for the life of me find a solution for how to get this to work. Please help?

2
  • What is not working? Is it that the keyboard is not hidden? Is it that it is not able to detect which key was pressed? Commented Nov 25, 2018 at 21:24
  • Yes. The keyboard doesn't hide when a key is pressed. Commented Nov 25, 2018 at 22:00

1 Answer 1

1

This is what you can try doing. Set an onKeyListener on your EditText to see what key was pressed like this:

editText.setOnKeyListener(new OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (editText.getText().toString().length() == 1) { 
            hideKeyboard(MainActivity.this, <Your view here>);
        }
        return true;
    }
});

Try changing your hideKeyboard function to the following:

public static void hideKeyboardFrom(Context context, View view) {
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
Sign up to request clarification or add additional context in comments.

6 Comments

Are you still having the same behavior?
I think the problem may be with your hideKeyboard function.
I'm confused why it's not working. I think the problem must be with hiding the keyboard, not the code for checking if 'enter' was pressed.
I don't think I have been clear enough about what I want to achieve. I'm not having an issue with the ENTER button, that is working fine. I want the keyboard to hide after the user touches whichever letter they are guessing. So, if the user taps the 'E' key, the keyboard will hide as soon as the 'E' char appears in the EditText field.
Oh! I see! I thought that it was they enter the letter then hit enter. Ok.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.