1

I'm using a SearchView to enter a number from a bluetooth barcode reader.

The SearchView is focused as follows:

svActListaPedidosFragmento.setFocusable(true);
svActListaPedidosFragmento.setIconified(false);

The problem is that when the SearchView is focused, the software keyboard appears and I want to hide it.

The method to hide the keyboard I understand very well

The problem is that I can not find an event that tells me that the keyboard has appeared.

In a previous thread someone told me that I can use the OnClickListener event for SearchView, but I discovered that this event happens BEFORE the keyboard is displayed.

Thanks for any comments or suggestions.

Fabián Romo
  • 299
  • 2
  • 12

2 Answers2

2

Just Use this and pass your editText and boolean type param to show or keyboard

private void inputModeChange(final EditText editText, final boolean showKeyboard) {
        editText.postDelayed(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                if (showKeyboard) {
                    InputMethodManager keyboard = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                    keyboard.showSoftInput(editText, 0);
                } else if (showKeyboard == false) {
                    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);

                }
            }
        }, 50);
    }

Hope it will Help.

Sandeep Parish
  • 1,698
  • 1
  • 6
  • 17
  • 1
    Thank you very much for the response, indeed the event happens after the keyboard appears but when trying to hide it doesn't disappear, I have tried to do so: svActListaPedidosFragmento.postDelayed(new Runnable() { @Override public void run() { final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); if (imm!=null){ imm.hideSoftInputFromWindow(getView().getWindowToken(), 0); } } }, 100) – Fabián Romo Aug 04 '18 at 19:27
  • I purchased that imm.hideSoftInputFromWindow returns a false value. – Fabián Romo Aug 04 '18 at 19:28
  • I have realized that it is time, I used 100 ms, but with 500 ms it worked correctly – Fabián Romo Aug 04 '18 at 19:38
  • Thanks Fabian, I was struggling with the same issue and actually I solved it but by using many ugly tricks for saying so, I tried a bunch of methods for hiding the softkey but none of them worked, the key was hiding it after a few seconds by using the postDelayed stuff, as you did, so implemented your snippet and it worked great. Thanks man. – Jose Ricardo Citerio Alcala Jan 13 '20 at 01:44
1

I'm currently creating an app and I've got a function to hide the keyboard when it's focused on an EditText. Here it is:

static void editTextHideKeyboard(Context context, EditText editText) {
    InputMethodManager imm = (InputMethodManager) context.getSystemService(LoginActivity.INPUT_METHOD_SERVICE);
    if (imm!=null)imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}

Hope this helps !

Edit: I've found out that the code I posted isn't mine and I've just copy-pasted from another StackOverflow post! Here is the original post, and all credit goes to the original poster!

Neïl
  • 76
  • 8
  • Thanks for write. Exactly that same process I use to hide the keyboard, but the problem is I don't find an event that indicates that the keyboard has appeared. – Fabián Romo Aug 04 '18 at 13:50
  • @FabiánRomo Oh yeah I see... If the `onClickEvent` occurs before the keyboard is shown, have you tried waiting a certain amount of time before hiding the keyboard? Maybe the time between the text field being focused and the keyboard being shown is always the same – Neïl Aug 04 '18 at 13:56
  • That's an idea that has been spinning around in my head, that is, activating a timer in the onClickEvent event so that when that timer overflows it will verify if the keyboard is present and hide it, but I think it's a cumbersome solution – Fabián Romo Aug 04 '18 at 13:58
  • @FabiánRomo: Check [this StackOverflow post](https://stackoverflow.com/a/16480207/7507743), it may help you! I haven't tried it myself but judging by the comments it may be the solution. – Neïl Aug 04 '18 at 14:01