0

We have an application that runs on a barcode scanner android device (ScanSKU). And whenever the user is scanning anything the keyboard will show up as it "types" the text they scanned, and will block half of the screen.

Our application is basically a WebView and we still need to be able to focus form elements inside of it.

But is there anyway we can build a toggle to disable the on-screen keyboard from coming up while they are in the WebView?

I've tried using this:

// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {  
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

From: Close/hide the Android Soft Keyboard

But with no success.

Any suggestions or ideas on how I can go about this?

Kalariya_M
  • 1,055
  • 8
  • 20
ECourant
  • 108
  • 1
  • 13
  • Possible duplicate of [Close/hide the Android Soft Keyboard](https://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard) – nhoxbypass Nov 07 '17 at 14:18

1 Answers1

2

There is another way how to get focus view, getCurrentFocus method didn't work for me too. Try this solution, you should provide instance of Activity object.

public static void hideSoftKeyboard(Context context) {
    try {
        InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(((Activity) context).getWindow().getDecorView().getRootView().getWindowToken(), 0);
        ((Activity) context).getWindow().getDecorView().getRootView().clearFocus();
    } catch (NullPointerException e) {
        // some error log 
    }
}

Edited: If you use WebView and still need to be able to focus form elements inside of it. You can set attribute

view.setFocusableInTouchMode(false);

for another views on the screen and WebView will hold focus on it.

Victor V.
  • 328
  • 2
  • 7
  • So if I were to call this function from my activity, like in onKeyDown. Would I just call it like this: `hideSoftKeyboard(this);` this being the activity? It seems to not work, as soon as I scan something the keyboard comes back up? Or am I using this wrong? – ECourant Nov 07 '17 at 16:20
  • Yes, `this` is Activity. Method should close keyboard one time, when you call it, but not blocking keyboard next time. Also, I have another suggestion, i will add it to answer. – Victor V. Nov 07 '17 at 17:16
  • By putting this method into a javascript interface for the web view. And calling this whenever there was a focus change in html, as well as when there was a hardware keyboard input (something was scanned) it kept the keyboard hidden. Thank you very much. – ECourant Nov 07 '17 at 20:06