1

I need to show/hide the Android soft keyboard programmatically in a WebView. The user has a button to show/hide it. I've read a few posts on how to hide it but none about a toggle feature, so that the user can hide or show it on demand and use only the hardware keyboard where present, or barcodes read by the embedded scanner of my device.

Sergiob
  • 534
  • 6
  • 15

1 Answers1

0

Ok, in the end nobody replied to me so I've found a workaround. It's not the best possibile but at least in my case solved the problem. I needed to disable/enable the keyboard on demand (through a function key from a Zebra rugged device), what makes things more complicated is that this feature should work with a WebView where, programmatically, there is no easy (...or at all) way to gain access of any EditText control embedded in it. The work around is not perfect nor elegant, but it works for me, so I wanted to share with others that maybe are facing similar issues. First of all we must detect touches inside the WebView (in KOTLIN):

       webView.setOnTouchListener(object : View.OnTouchListener {
            override fun onTouch(v: View, m: MotionEvent): Boolean {

                if (!keybEnabled) {

                    hideKeyboard()
                }

                return false
            }
        })

Then I added a scheduled task to hide the keyboard:

    private fun hideKeyboard() {

        Handler().postDelayed({

            val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.hideSoftInputFromWindow(webView.getWindowToken(), 0)

        }, 200)
    }

The method hideKeyboard() will forcibly hide the keyboard until the user hits a button to enable it (flag keybEnabled == true).

There are 2 drawbacks with this solution. 1) If you reduce the delay to a value close to 100 ms it is possibile that the soft keyboard is hidden before Android is starting the task to display it, so it won't work. 200 ms as in the example is enough for my situation, but it depends on the device's performance. 2) Sometimes the soft keyboard appears on the screen for a few milliseconds before disappearing (only when the user taps on an EditText).

If point 2 is not a problem for you, this solution should work fine. Hope it will help others.

UPDATE

I've found a slightly different solution that solves also the keyboard appearance described above. It is at this link Handler Solution.

FINAL CONSIDERATIONS

I think that soft keyboard management in Android is horrible, it is unbelievable that after so many years it is still based on such a cumbersome logic. There isn't a function to simply disable/enable the soft keyboard programmatically and for all the current activity's controls, it's even worse that there isn't any SDK function that returns the soft keyboard status (displayed/hidden). I think that is simply unacceptable because I do not think that Android development team would spend more the 2 hours to make available such useful features.

Sergiob
  • 534
  • 6
  • 15