Android: How to hide/show keyboard while focused on EditText?

3.4k Views Asked by At

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()
1

There are 1 best solutions below

0
Timchang Wuyep On

Java

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

Kotlin

window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)