Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Hiding the keyboard in an overridden "Done" keypress of EditText

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?

like image 658
marshall.ward Avatar asked Apr 18 '10 02:04

marshall.ward


3 Answers

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.

like image 75
just_another_coder Avatar answered Oct 11 '22 04:10

just_another_coder


You can close the keyboard by doing:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindowToken(), 0);
like image 42
Macarse Avatar answered Oct 11 '22 03:10

Macarse


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);
like image 8
nish Avatar answered Oct 11 '22 03:10

nish