Scheming with the Coco and Android

March 1 2013
This is a Example of a way to handle the enter key in EditTexts using a TextView.OnEditorActionListener. It has an advantage over other methods in that it responds to soft keyboard enters, too. EnterKey.zip
This is how I created the Ant outline:

android create project --target "Google Inc.:Google APIs:17" --name BlueWord --path C:/Users/Earl/Documents/EnterKey --activity EnterKey --package org.nuts4cocos.enterkey

From main.xml

  ...
  <EditText android:id="@+id/editText1"
    ...

EnterKey.java

public class EnterKey extends Activity {
  TextView.OnEditorActionListener listener;
  EditText editText1;
  EditText editText2;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    editText1=(EditText)findViewById(R.id.editText1);
    editText2=(EditText)findViewById(R.id.editText2);

    listener=new TextView.OnEditorActionListener() {
      @Override
      public boolean onEditorAction
      (TextView view, int actionId, KeyEvent event) {
        if (event==null) {
          if (actionId==EditorInfo.IME_ACTION_DONE);
          // Capture soft enters in last singleLine EditText.
          else if (actionId==EditorInfo.IME_ACTION_NEXT);
          // Capture soft enters in other singleLine EditTexts
          else return false;  // Let system handle all other null KeyEvents
        }
        else if (actionId==EditorInfo.IME_NULL) {
          // Capture most soft enters in multi-line EditTexts and hard enters.
          // They supply a zero actionId and a valid KeyEvent rather than
          // a non-zero actionId and a null event like the previous cases.
          if (event.getAction()==KeyEvent.ACTION_DOWN); 
          // We capture the event when key is first pressed.
          else  return true;   // We consume the event when key is released.
        }
        else  return false; 
        // We let the system handle it when the listener
        // is triggered by something that wasn't an enter.

        // Code from this point on will execute whenever the user
        // presses enter in an attached view, regardless of position, 
        // keyboard, or singleLine status.
        if (view==editText1)  editText1.append("1");
        else if (view==editText2)  editText2.append("2");
        else {
          Log.e("EnterKey", "onEditorAction view wasn't of our EditTexts");
          return false;
        }
        return true;   // Consume the event
      }
    };
    editText1.setOnEditorActionListener(listener);
    editText2.setOnEditorActionListener(listener);
  }
}
§386 · By · February 26, 2013 ·


9 Comments to “EditText Enter Key”

  1. Arthur says:

    Many thanks for your code example – I have been struggling with EditText and soft keyboard input for several days before finding your blog.

    There are a couple of problems in your zipped code:

    The manifest does not set the minimum or target SDK version, which results in a target of Android 1.5 (on my Eclipse set-up). The code runs fine, but with any other settings ‘setOnEditorActionListener’ complains about minimum SDK requirements (Android 3) and the code does not work – – the ‘onEditorAction’ is just never called. I solved this by changing the default ime action, adding
    android:imeOptions=”actionGo”
    to the ‘EditText’ in the layout xml. Doing this restored the example to working in Android versions 2.2 through 17.

    What I would like to accomplish is to provide a method to allow either a return character or an “editing completed” result from the same return key – allowing the use of multiline text and not requiring an additional ‘Done’ button in the layout. I have experimented with looking for the shift state within the ‘onEditorAction’ method (so that a ‘Shift + Return’ could be used to close the soft keyboard and a simple ‘Return’ sent back to the EditText). Unfortunately, the ‘KeyEvent’ does not seem to include any of the expected values. My LogCat is providing the same results when the shift state is on or off.

    LogCat: (these are all the potential values I could find)
    KeyCode is: 66
    Flags are: 22
    Contents are: 0
    MetaState is: 0
    UnicodeChar is: 10
    Shift is Not Pressed

    This is exceptionally annoying! I have not finished with testing, and may try to detect the shift key state in the view and set a flag. Have you tried anything like this?

  2. Earl says:

    stackoverflow left a link in my profile : http://stackoverflow.com/faq#promotion so I had to remove my links from the two answers that I had put them in. Thanks for showing how to make this example work in eclipse. I haven’t tried detecting the shift key state in the view. I just thought of a way to handle the situation. I’ll post my results tomorrow.

  3. earlcasper says:

    You could use the empty enter like I do in the project that I am developing, for a trigger like a shift enter. The user signals he is done by hitting two enters in a row. It also uses a TextWatcher.
    Updated 4/17/2013

  4. earlcasper says:

    I didn’t like my comment before last so I proceeded to edit it. My son said that I shouldn’t have and I agree. So now that my son has found some errors in my EmptyEnter program, I’m posting the better version in a new comment. EmptyEdit.zip In addition to the OnEditorActionListener and TextWatcher it has an OnTouchListener. It reports the type of <enter> in an EditText above the main one. If it is an isolated <enter> it goes into the main EditText. If it is immediately followed by another the first is deleted and <empty-enter> is reported in the enterType EditText.

  5. earlcasper says:

    I wrote a post about a better way I have found for handling <enter-key>s and extended it in a post to double <enter-key>s.

  6. Stephen H. Fischer says:

    Hi,

    CoCo3.com has a busted link for Urbane.zip on one of it’s two Urbane pages. Owner is not paying attention to his site anymore IMHO.

    I am looking at producing an updated version to be archived on http://www.colorcomputerarchive.com/

    May I include your latest URBANE version and tell folks on Facebook https://www.facebook.com/groups/2359462640/?fref=ts about your work. ~990 members now.

    Stephen H. Fischer

  7. earlcasper says:

    Unfortunately I suffer from bipolar disorder. My social worker says great men have also suffered it, like Abraham Lincoln and Winston Churchill. The way it works with me I go years down in the doldrums with occasional spurts of high activity. The last one was when I was working with you on a c version offshoot of your CoCo3 source version. It really felt good seeing your name again. Hopefully I can stay high long enough for a CoCo3 immediate version. -earlcasper

  8. earlcasper says:

    Hello World. Here I am again. This time I’m trying for a CoCo 2 immediate version. So far I have a character and screen editor. Loosely based on FLEXIKEY. The rules are as follows.
    **********************************************
    * LEFT ARROW MOVES LEFT ONE SPACE.
    * RIGHT ARROW MOVES RIGHT ONE SPACE.
    * SHIFT-LEFT ARROW IS BKSP.
    * SHIFT-RIGHT ARROW IS DEL.
    *
    * UP ARROW MOVES UP ONE LINE.
    * DOWN ARROW MOVES DOWN ONE LINE.
    * SHIFT-UP ARROW TOGGLES INSERT MODE.
    * SHIFT-DOWN ARROW DELETES CURRENT LINE.
    * ENTER OPENS A LINE BELOW THE CURRENT LINE.
    *
    **********************************************

  9. Earl Winston Casper says:

    I kept deleting lines when I was trying to print [‘s so I changed it to ALT then ENTER. I was exiting then so I traded with SHIFT-UP. I figure fewer people will be trying to print up-arrows.
    *************************************************
    * LEFT ARROW MOVES LEFT. RIGHT MOVES RIGHT
    * SHIFT-LEFT IS BKSP. SHIFT-RIGHT IS DEL
    *
    * UP MOVES UP. SHIFT-DOWN TOGGLES INSERT
    * DOWN MOVES DOWN. SHIFT-UP EXITS
    * ENTER OPENS LINE. ALT THEN ENTER DELETES
    *
    * ALT IS SHIFT-CLEAR. ON VCC IT IS SHIFT-HOME
    * ALT THEN SHIFT-RIGHT IS ]. ALT THEN ALT IS \
    * ALT THEN SHIFT-DOWN IS [. ALT THEN UP IS
    * UP-ARROW. ALT THEN SHIFT-UP IS LEFT-ARROW
    *************************************************

Leave a Reply