skip to Main Content

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?

2

Answers


  1. 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 !!! :~)

    Login or Signup to reply.
  2. activity?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)

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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search