A quick article on how to replace the return key on a soft keyboard (software based) on a touch-device.
Why?
I have an editText where a user can enter any text value but if they typed a return character, the app would crash because it didn't understand the data. I tried replacing the new lines but just stopping the return key or trying to catch it is an obsolete method.
How?
The following example replaces the return key with a "GO" key:
The XML
<EditText
android:id="@+id/myEditText"
android:hint="Location: City, Zip ..."
android:imeOptions="actionGo"
android:inputType="text">
</EditText>
The Java
EditText location = (EditText)findViewById(R.id.editTextLoc);
location.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_GO) {
openMap(v); // do function on pressing the word GO
handled = true;
}
return handled;
}
});
Other Noteworthy Actions
android:inputType
In some cases, this will change the keyboard layout (eg. Numbers)
text // Normal text
textNoSuggestions // No suggestions/autocorrect
textEmailAddress // valid email address
textPersonName // name of a person
textPostalAddress // standard postal address
textPassword // password type field
textVisiblePassword // um...
textPhonetic // text that will sound like
number // changes keyboard to numeric
numberDecimal // allow a decimal number
phone // enter a phone number
datetime // enter a date and time
date // selectable date
time // selectable time
android:imeOptions
normal
actionUnspecified
actionNone
actionGo
actionSearch
actionSend
actionNext
actionDone
actionPrevious
IME_ACTION(s)IME_ACTION_DONE // the action key performs a "done" operation, typically meaning there is nothing more to input and the IME will be closed.
IME_ACTION_GO // the action key performs a "go" operation to take the user to the target of the text they typed.
IME_ACTION_NEXT // the action key performs a "next" operation, taking the user to the next field that will accept text.
IME_ACTION_NONE // there is no available action.
IME_ACTION_PREVIOUS // like IME_ACTION_NEXT, but for moving to the previous field.
IME_ACTION_SEARCH // the action key performs a "search" operation, taking the user to the results of searching for the text they have typed (in whatever context is appropriate).
IME_ACTION_SEND // the action key performs a "send" operation, delivering the text to its target.
IME_ACTION_UNSPECIFIED // no specific action has been associated with this editor, let the editor come up with its own if it can.
Source(s)
Discussion
Comments
Questions, corrections and useful additions are reviewed before appearing here.
No comments yet. Start the discussion.