Home > front end >  Space between soft keyboard and edit text
Space between soft keyboard and edit text

Time:04-14

I need to provide some padding between the keyboard and edittext when soft keyboard pops up which generally hides the bottom part of edittext.

When the soft keyboard is opened, i need the full edittext view to be visible. And more importantly, I can’t go with android:windowSoftInputMode = "adjustResize", which doesn’t work good in my use case. I am using "adjustPan".

So how to achieve the same without changing the windowSoftInputMode?

CodePudding user response:

Create this function for EditText in your class :

private fun View.focusAndShowKeyboard() {

        fun View.showTheKeyboardNow() {
            if (isFocused) {
                post {
                    val imm =
                        context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                    imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
                }
            }
        }

        requestFocus()
        if (hasWindowFocus()) {
            showTheKeyboardNow()
        } else {
            viewTreeObserver.addOnWindowFocusChangeListener(
                object : ViewTreeObserver.OnWindowFocusChangeListener {
                    override fun onWindowFocusChanged(hasFocus: Boolean) {
                        if (hasFocus) {
                            [email protected]()
                            viewTreeObserver.removeOnWindowFocusChangeListener(this)
                        }
                    }
                })
        }
    }

and you can use it like this :

txtValue.focusAndShowKeyboard()

That's it. Happy coding !!! :~)

CodePudding user response:

activity?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)

Use this in OnResume method of your fragment It works for me

  • Related