Show Android Soft Keyboard Programmatically
Dec 6, 2020
In my last project I need to show Android soft keyboard automatically on a particular EditText when user navigate to a screen. The working code for my app is this two lines of code:
// 1
editText.requestFocus()
// 2
(getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
- We request a focus on the EditText so if the keyboard visible and user enter some data, this EditText value will change accordingly.
- We request system service to force soft input to be shown. There are other code tha could do the same, but this one is tested working on Xiaomi, so I use this one.