How to dynamically enable and disable the android soft keyboard

90 Views Asked by At

I am loading a certain web page in my android application using a webview and I want to disable the android keyboard for some screens and enable the android keyboard for some screens. So I tried implementing several approaches non of them work. When I set all the properties to support the keyboard enable and then try to disable it, it still pops out. These are the things I have done,

class SampleWebview(context: Context, attrs: AttributeSet): WebView(context, attrs) {
    override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection? {
        return null
    }

    override fun onCheckIsTextEditor(): Boolean {
        println("onCheckIsTextEditor")
        return false
    }
}

I tried setting the onCheckIsTextEditor to false but it still keeps popping out. Please note that the above created SampleWebview was used in the layout file and references inside the Main Activity as well. Then I tried this,

wb.webViewClient = object: WebViewClient(){

            override fun onPageFinished(view: WebView?, url: String?) {
                viewModel.onPageFinishedLoading()

            }

            override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                if(url?.contains("/hide", true) == false){
                    wb.isFocusable = true
                    wb.isFocusableInTouchMode = true
                    pt.descendantFocusability = ViewGroup.FOCUS_BEFORE_DESCENDANTS
                } else {
                    wb.isFocusable = false
                    wb.isFocusableInTouchMode = false
                    pt.descendantFocusability = ViewGroup.FOCUS_BLOCK_DESCENDANTS

                }
            }
}

But this doesn't work as well. But if i set the

wb.isFocusable = false
wb.isFocusableInTouchMode = false
pt.descendantFocusability = ViewGroup.FOCUS_BLOCK_DESCENDANTS

and doesn't change them then the soft keyboard is disabled for the entire application (which is great), but I need the soft keyboard to be enabled for some screens in the webview. How to achieve this? I have been trying different solutions for hours now, and yet no exact solution. Any guidance for this is much appreciated.

To sum up things again, what i need is to enable the android soft keyboard for some screens in the website and to disable it on some screens in the website which is displayed inside the Webview.

1

There are 1 best solutions below

1
Camp Nerd On

Let's look at this at another approach. You can hide the keyboard by making the keyboard lose focus. You can also use this as an example to you code. It uses a button and an edit text.

MainActivity : AppCompatActivity() {
// on below line creating variables
lateinit var hideBtn: Button
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    // on below line wea re initializing our variables.
    hideBtn = findViewById(R.id.idBtnHide)
    
    // on below line adding click listener for button.
    hideBtn.setOnClickListener {
         
        // on below line getting current view.
        val view: View? = this.currentFocus
         
        // on below line checking if view is not null.
        if (view != null) {
            // on below line we are creating a variable
            // for input manager and initializing it.
            val inputMethodManager =
                getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
             
            // on below line hiding our keyboard.
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0)
             
            // displaying toast message on below line.
            Toast.makeText(this, "Key board hidden", Toast.LENGTH_SHORT).show()
        }
    }
}
}

Using the input manager will cause the keyboard to lose focus and hide. You can also use this in the main portion to where you tap anywhere on the screen and the keyboard will hide.

This is a method of hiding (not disabling) the keyboard.