How do I handle ImeOptions' done button click?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
On Android, imeOptions controls what action button the soft keyboard shows, but it does not by itself implement the behavior behind that button. If you want to react to the Done action, the normal solution is to set the input field's IME option and then listen for the corresponding editor action in code. The important distinction is that the XML changes keyboard presentation, while the listener handles application behavior.
Set the IME Action on the Input Field
Start by telling the EditText that it should present a Done-style action.
That affects the keyboard's action button label and intent, but it does not automatically submit a form, hide the keyboard, or move focus. You still have to define what “done” means on this screen.
Handle the Action in Kotlin
The common pattern is setOnEditorActionListener combined with a check for EditorInfo.IME_ACTION_DONE.
Returning true tells Android that your code handled the event. Returning false lets the framework or keyboard continue default handling.
Hide the Keyboard and Clear Focus
Many screens want the Done action to conclude editing cleanly. That usually means hiding the keyboard and removing focus from the field.
Without those follow-up steps, the UI may technically handle the action but still feel unfinished to the user.
Some Keyboards Send Enter-Key Events Too
Different keyboard apps do not always behave identically. In some cases you may also receive a KeyEvent for the Enter key. If you need more defensive handling, check both paths.
You do not always need this extra check, but it is useful when behavior varies across devices or keyboards.
Decide What Done Means on This Screen
The listener should not just detect a button press. It should map the keyboard action to the screen's intent.
Common meanings include:
- submit a login form
- validate the current field
- hide the keyboard after editing
- trigger a search or filter action
- move the workflow to the next screen state
Being explicit about that meaning makes the code much easier to maintain than scattering small keyboard handlers with no clear UI contract.
Keep the Listener Close to the UI Logic
It is tempting to put IME handling into a generic helper immediately, but the behavior often belongs to the specific screen. A login screen may submit. A comment box may insert the final content. A settings field may just hide the keyboard.
Reusable helpers are good for mechanical tasks such as keyboard dismissal, but the business action behind Done should usually stay near the screen code that owns the input.
Common Pitfalls
- Setting
android:imeOptions="actionDone"and expecting it to handle the button press automatically. - Returning
falseeven though the app already handled the action, which can lead to duplicate behavior. - Ignoring device or keyboard variations where an Enter key event appears alongside or instead of the editor action.
- Hiding the keyboard without clearing focus, which leaves the field in an awkward half-active state.
- Treating
Doneas a visual label only instead of mapping it to a specific screen-level action.
Summary
- '
imeOptionschanges the keyboard action button, but behavior is implemented in code.' - Use
setOnEditorActionListenerand check forEditorInfo.IME_ACTION_DONE. - Return
truewhen your code consumes the event. - Hiding the keyboard and clearing focus are common follow-up steps.
- The right handler depends on what “done” actually means in that specific UI flow.
Related reading
- How do I hide a menu item in the actionbar?
- How do I hide the status bar in a Swift iOS app?
- How do I hide the status bar in a Swift iOS app?
- How do I implement an Objective-C singleton that is compatible with ARC?
- How do I import a Swift file from another Swift file?
- How do I inspect the view hierarchy in iOS?
- How do I launch the Android emulator from the command line?
- How do I load an HTTP URL with App Transport Security enabled in iOS 9?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.