Kotlin Extension Solution
The base way to handle the done action in Kotlin is:
edittext.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
// Call onDone result here
true
}
false
}
Kotlin Extension
Use this to call edittext.onDone {/*action*/}
in your main code. Keeps it more readable and maintainable
edittext.onDone { submitForm() }
fun EditText.onDone(callback: () -> Unit) {
// These lines optional if you don't want to set in Xml
imeOptions = EditorInfo.IME_ACTION_DONE
maxLines = 1
setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
callback.invoke()
true
}
false
}
}
Don't forget to add these options to your edittext Xml, if not in code
<EditText ...
android:imeOptions="actionDone"
android:inputType="text"/>
If you need inputType="textMultiLine"
support, read this post