14

I have used a bit of Android code to override the "Done" button in my EditText field:

   myEditField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {

                mySubroutine();

                return true;
            }
            return false;
        }
    });

Activating the field calls up the keyboard, and pressing "Done" evaluates mySubroutine() successfully. However, the keyboard no longer goes away when I press "Done". How do I restore this default behaviour to the routine?

Cœur
  • 32,421
  • 21
  • 173
  • 232
marshall.ward
  • 5,652
  • 7
  • 31
  • 50

4 Answers4

53

Why not:

myEditField.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
    @Override 
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
        if (actionId == EditorInfo.IME_ACTION_DONE) { 

            mySubroutine(); 
        } 
        return false; 
    } 
}); 

Just return false after you handle your code. This can be interpreted as no matter what your code (mySubroutine()) does it will still use the default action afterwards. If you return "true" you are telling that you are a happy coder and everything that needed to be done has happen in your mySubroutine() and the default action do not need to take action.

Norfeldt
  • 5,230
  • 13
  • 70
  • 118
just_another_coder
  • 1,136
  • 2
  • 13
  • 21
  • This is the correct way as otherwise you will be going counter to how the framework was designed. I've elaborated on that in the _way-over-the-top_ answer in a related question [here](http://stackoverflow.com/a/25119481/2837443) – Alex.F Aug 04 '14 at 13:14
  • I haven't touched Android code in four years, so I'll take your word for it. – marshall.ward Jan 19 '15 at 11:09
13

You can close the keyboard by doing:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindowToken(), 0);
Phileo99
  • 5,206
  • 2
  • 45
  • 52
Macarse
  • 87,001
  • 42
  • 169
  • 229
  • I'm using Eclipse and I'm getting the two following errors: "InputMethodManager cannot be resolved to a type" and "the method getWindowToken() is undefined for the type new TextView.onEditorActionListener(){}" I'm not much of a Java programmer so I don't know how to interpret these. Do I need to include something earlier? – marshall.ward Apr 18 '10 at 19:15
  • Try pressing Control+Shift+o to do the corresponding imports. – Macarse Apr 18 '10 at 20:15
  • 5
    Thanks, there was a dependency that I needed. I also had to replace getWindowToken() with v.getWindowToken() but otherwise it works great, thanks! Now I just need to understand what it did – marshall.ward Apr 18 '10 at 23:00
8

You must attach an onClickListener to the button that executes the following code:

InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editview.getWindowToken(), 0);
Jeshurun
  • 21,639
  • 5
  • 75
  • 88
nish
  • 119
  • 1
  • 6
0

I had the same problem. Immediately after editText VISIBILITY change from GONE to VISIBLE, I had to set the focus and display the soft keyboard. I achieved this using the following code:

        (new Handler()).postDelayed(new Runnable() {

        public void run() {              yourEditText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
            yourEditText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));                       

        }
    }, 200);
Hai Rom
  • 1,511
  • 13
  • 9