138

I have an Activity with an EditText, a button and a ListView. The purpose is to type a search screen in the EditText, press the button and have the search results populate this list.

This is all working perfectly, but the virtual keyboard is behaving strange.

If I click the EditText, I get the virtual keyboard. If I click the "Done" button on the virtual keyboard, it goes away. However, if I click my search button before clicking "Done" on the virtual keyboard, the virtual keyboard stays and I can't get rid of it. Clicking the "Done" button does not close the keyboard. It changes the "Done" button from "Done" to an arrow and remains visible.

Thanks for your help

K Guru
  • 1,292
  • 2
  • 17
  • 36
Andrew
  • 20,756
  • 32
  • 99
  • 177

14 Answers14

312
InputMethodManager inputManager = (InputMethodManager)
                                  getSystemService(Context.INPUT_METHOD_SERVICE); 

inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                                     InputMethodManager.HIDE_NOT_ALWAYS);

I put this right after the onClick(View v) event.

You need to import android.view.inputmethod.InputMethodManager;

The keyboard hides when you click the button.

Paul Maserrat
  • 3,411
  • 3
  • 22
  • 24
  • 55
    Note: (in case you want to use this method in instances where there might not be focus (e.g. onPause(), etc): `inputManager.hideSoftInputFromWindow((null == getCurrentFocus()) ? null : getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);` – Peter Ajtai Feb 16 '12 at 01:37
  • 5
    You also have to import Context. – Si8 Jul 22 '13 at 18:19
  • 4
    CAUTION: Throws NPE if keyboard is already hidden. Follow Peter's comment to avoid this. – Don Larynx May 21 '15 at 08:47
  • why is the keyboard showing up after a click on an irrelevant button ? can someone provide some explanation or a link ? – kommradHomer Jan 25 '16 at 11:22
  • 1
    Works like charm ! – ARiF Sep 02 '16 at 14:44
60
mMyTextView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            // hide virtual keyboard
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(m_txtSearchText.getWindowToken(), 
                                      InputMethodManager.RESULT_UNCHANGED_SHOWN);
            return true;
        }
        return false;
    }
});
JJD
  • 50,076
  • 60
  • 203
  • 339
Andrew
  • 20,756
  • 32
  • 99
  • 177
33

Use Below Code

your_button_id.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        try  {
            InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        } catch (Exception e) {

        }
    }
});
Ziem
  • 6,579
  • 8
  • 53
  • 86
13

You should implement OnEditorActionListener for your EditView

public void performClickOnDone(EditView editView, final View button){
    textView.setOnEditorActionListener(new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(EditView v, int actionId, KeyEvent event) {
            hideKeyboard();
            button.requestFocus();
            button.performClick();
            return true;
        }
    });

And you hide keyboard by:

public void hideKeybord(View view) {
    inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),
                                  InputMethodManager.RESULT_UNCHANGED_SHOWN);
}

You should also fire keyboard hiding in your button using onClickListener

Now clicking 'Done' on virtual keyboard and button will do the same - hide keyboard and perform click action.

JJD
  • 50,076
  • 60
  • 203
  • 339
pixel
  • 24,905
  • 36
  • 149
  • 251
  • Excellent, but I'm not quite following. I think the post ate some of the code (there's nothing after "public void" in your sample). I attempted to setOnEditorActionListner in my Activity's onCreate method, but it doesn't know what setOnEditorActionListener is. I get an "Anonymous inner type" notification. (I'm doing this in my Activity onCreate method) http://i37.tinypic.com/6ozkig.png – Andrew Aug 03 '10 at 22:03
  • 1
    It looks like there are a couple of mistakes in this code, but it's the right idea. For one thing, the OnEditorActionListener interface is an inner class, so you need to either import it explicitly (Eclipse won't do it for you in this case) or refer to it as `TextView.OnEditorActionListener`. – Tyler Aug 04 '10 at 05:21
  • I'm having a bit of trouble. I've implemented onEditorActionListener (public class SearchActivity extends ListActivity implements OnClickListener, OnEditorActionListener), I've attached a listener to my EditText (mSearchText.setOnEditorActionListener(this);), but Eclipse wont allow me to override the onEditorAction handler (public boolean onEditorAction(TextView v, int actionId, KeyEvent event)). It says it must override a superclass method. Any ideas? – Andrew Aug 04 '10 at 18:58
  • Hi, you can inline your OnEditorActionListener by typing yourEditView.setOnEditorActionListener(new OnEditorActionListener() {.... – pixel Aug 04 '10 at 21:33
12

In your case, as you only have one EditText, the bellow solution is the best one, I believe. If you had more than one EditText component, then either you would need to focus on the EditText that you wanted to close, or call the bellow function for every EditText. For you though, this works brilliantly:

editText.onEditorAction(EditorInfo.IME_ACTION_DONE);

Basically, the EditText already has a function that is intended to deal with what to do after the keyboard has been used. We just pass that we want to close the keyboard.

Gaurav Mall
  • 2,372
  • 1
  • 17
  • 33
Laert
  • 166
  • 1
  • 6
12

For Activity,

InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

For Fragments, use getActivity()

getActivity().getSystemService();

getActivity().getCurrentFocus();

InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
Priya Rajan
  • 687
  • 8
  • 21
11

Add the following code inside your button click event:

InputMethodManager inputManager = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
Ziem
  • 6,579
  • 8
  • 53
  • 86
Ashana.Jackol
  • 3,064
  • 28
  • 22
7

This solution works perfect for me:

private void showKeyboard(EditText editText) {
    editText.requestFocus();
    editText.setFocusableInTouchMode(true);
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(editText, InputMethodManager.RESULT_UNCHANGED_SHOWN);
    editText.setSelection(editText.getText().length());
}

private void closeKeyboard() {
    InputMethodManager inputManager = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
}
Giedrius Šlikas
  • 1,073
  • 12
  • 12
4

Try this...

  1. For Showing keyboard

    editText.requestFocus();
    InputMethodManager imm = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    
  2. For Hide keyboard

    InputMethodManager inputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE); 
    inputManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
    
1
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm =(InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);enter code here}
Farruh Habibullaev
  • 2,342
  • 1
  • 26
  • 33
  • Hi! Though this answer might suffice the needs of *@Andrew* it would be great, if you could extend it with some explaination, to make sure that future readers may benefit to the fullest of it! – derM - not here for BOT dreams May 04 '17 at 20:12
1

Kotlin example:

val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager

from Fragment:

inputMethodManager.hideSoftInputFromWindow(activity?.currentFocus?.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)

from Activity:

inputMethodManager.hideSoftInputFromWindow(currentFocus?.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
norbDEV
  • 4,795
  • 2
  • 37
  • 28
0

You use this code in your button click event

// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {  
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
Pankaj Talaviya
  • 3,328
  • 28
  • 31
0

Crash Null Point Exception Fix: I had a case where the keyboard might not open when the user clicks the button. You have to write an if statement to check that getCurrentFocus() isn't a null:

            InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if(getCurrentFocus() != null) {
            inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
CacheMeOutside
  • 699
  • 7
  • 24
-2

If you set android:singleLine="true", automatically the button hides the keyboard¡

Terranology
  • 610
  • 9
  • 15