Print

Android: Replace return key with done, go, send...

What?
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
copyraw
<EditText
        android:id="@+id/myEditText"
        android:hint="Location: City, Zip ..."
        android:imeOptions="actionGo"
        android:inputType="text">
</EditText>
  1.  <EditText 
  2.          android:id="@+id/myEditText" 
  3.          android:hint="Location: City, Zip ..." 
  4.          android:imeOptions="actionGo" 
  5.          android:inputType="text"> 
  6.  </EditText> 

The Java
copyraw
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;
        }
});
  1.  EditText location = (EditText)findViewById(R.id.editTextLoc)
  2.  location.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
  3.          @Override 
  4.          public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
  5.                  boolean handled = false
  6.                  if (actionId == EditorInfo.IME_ACTION_GO) { 
  7.                          openMap(v);  // do function on pressing the word GO 
  8.                          handled = true
  9.                  } 
  10.                  return handled; 
  11.          } 
  12.  })

Other Noteworthy Actions

android:inputType
In some cases, this will change the keyboard layout (eg. Numbers)
copyraw
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
  1.  text                    // Normal text 
  2.  textNoSuggestions       // No suggestions/autocorrect 
  3.  textEmailAddress        // valid email address 
  4.  textPersonName          // name of a person 
  5.  textPostalAddress       // standard postal address 
  6.  textPassword            // password type field 
  7.  textVisiblePassword     // um... 
  8.  textPhonetic            // text that will sound like 
  9.  number                  // changes keyboard to numeric 
  10.  numberDecimal           // allow a decimal number 
  11.  phone                   // enter a phone number 
  12.  datetime                // enter a date and time 
  13.  date                    // selectable date 
  14.  time                    // selectable time 

android:imeOptions
copyraw
normal
actionUnspecified
actionNone
actionGo
actionSearch
actionSend
actionNext
actionDone
actionPrevious
  1.  normal 
  2.  actionUnspecified 
  3.  actionNone 
  4.  actionGo 
  5.  actionSearch 
  6.  actionSend 
  7.  actionNext 
  8.  actionDone 
  9.  actionPrevious 
IME_ACTION(s)
copyraw
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.
  1.  IME_ACTION_DONE         // the action key performs a "done" operation, typically meaning there is nothing more to input and the IME will be closed. 
  2.  IME_ACTION_GO           // the action key performs a "go" operation to take the user to the target of the text they typed. 
  3.  IME_ACTION_NEXT         // the action key performs a "next" operation, taking the user to the next field that will accept text. 
  4.  IME_ACTION_NONE         // there is no available action. 
  5.  IME_ACTION_PREVIOUS     // like IME_ACTION_NEXT, but for moving to the previous field. 
  6.  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)
  7.  IME_ACTION_SEND         // the action key performs a "send" operation, delivering the text to its target. 
  8.  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)
Category: AndroidOS :: Article: 593