0

I have registration form and I need to show keyboard when user is focused in EditText. I had always problem with this and I used a lot of workarounds which are no longer working. Im surprised Android cant handle this basic thing like focusing between EditText on its own.

What is happening here: When I move from email EditText to password one, it will hide keyboard and password field is focused with indicator blinking without visible keyboard.

Code:

    emailEt.apply {
        onFocusChange {
            if (isFocused) app.showKeyboard(a) else app.hideKeyboard(this)
        }
        onEditorAction {
            passwordEt.requestFocus()
        }
        afterTextChanged {
            emailFieldValidation(true)
            validateData()
        }
    }

    passwordEt.apply {
        onFocusChange {
            if (isFocused) app.showKeyboard(a) else app.hideKeyboard(this)
        }
        onEditorAction {
            this.clearFocus()
        }
        afterTextChanged {
            passwordFieldValidation(true)
            validateData()
        }
    }

    resetData()
    emailEt.requestFocus()
martin1337
  • 2,384
  • 6
  • 38
  • 85
  • https://stackoverflow.com/questions/1109022/how-do-you-close-hide-the-android-soft-keyboard-programmatically and then use the **EditText** `isFocused()` method, to know when the **EditText** is focused. – Noah Feb 25 '22 at 10:40
  • Android does handle the keyboard on its own perfectly. But if you start messing with it, it's very easy to break it. Especially SHOW_FORCED takes all the bets off and you're managing the keyboard on your own (eg if your app gets killed, the keyboard stays on launcher). Try your layout in a fresh app, as long as EditTexts have `clickable` and `focusable` set to true, they will work on their own. Also, try on a different phone. If a hardware keyboard is hooked up (USB, BT, on emulator) then soft keyboard shouldn't open, that's a feature not a bug. – Agent_L Feb 25 '22 at 10:49
  • Well I unplugged phone from USB but its still not working. If I remove all of those focuschange listeners and let it handle it on its own, it will not even open keyboard while focused to first EditText on screen start. – martin1337 Feb 28 '22 at 07:23

1 Answers1

1

Java

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

Kotlin

window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
Timchang Wuyep
  • 629
  • 7
  • 10