Reputation: 676
I have a createEditText
function that creates an EditText
and adds it to the view. My issue is that once it is added to the view, the user has to tap the EditText
in order for the keyboard to be called and editing to work. What I am trying to do is to have it so that once the EditText
is created, the user is automatically taken to the edit mode.
In IOS programming, there is a function called becomeFirstResponder()
which achieves this. What would the android equivalent be?
Things I have tried:
myEditText.requestFocus()
myEditText.isActivated
myEditText.isFocused
myEditText.isSelected
myEditText.isEnabled
Upvotes: 3
Views: 11087
Reputation: 4673
This is just an idea (a bit pseudo-code just to clarify things):
myEditText.requestFocus()
(activity or dialog).window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
You must take care of not showing the soft keyboard if there is a hardware keyboard connected
Upvotes: 1
Reputation: 35264
Unfortunately it is not enough to call only EditText#requestFocus
. In addition to this you also have to call the InputMethodManager#showSoftInput
. Following utility method should work:
fun openSoftKeyboard(context: Context, view: View) {
view.requestFocus()
// open the soft keyboard
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
}
Upvotes: 6
Reputation: 1792
Add <requestFocus />
in your your EditText view in xml file of layout.
Upvotes: 0