76

I have a EditText and button aligned to parent's bottom.

When I enter text in it and press the button to save data, the virtual keyboard does not disappear.

Can any one guide me how to hide the keyboard?

Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83
UMAR-MOBITSOLUTIONS
  • 77,236
  • 95
  • 209
  • 278

15 Answers15

148

You might also want to define the imeOptions within the EditText. This way, the keyboard will go away once you press on Done:

<EditText
    android:id="@+id/editText1"
    android:inputType="text"
    android:imeOptions="actionDone"/>
sfmirtalebi
  • 370
  • 7
  • 16
angelrh
  • 1,491
  • 1
  • 9
  • 4
  • 49
    This will only hold true if you do not consume the event when implementing `onEditorAction([...])`. Returning `true` will prevent the keyboard from hiding correctly. – Eric Tobias Feb 14 '13 at 09:36
  • 9
    The soft keyboard will dismiss once press 'Done' or 'Search' either for actionDone or actionSearch, but you have to return false in onEditorActionListener. – alexhilton Feb 27 '14 at 16:09
  • 7
    This and `android:singleLine="true"`. – Simas Dec 22 '14 at 13:06
  • If the user does not press "Done" but dismisses the soft keyboard using the "v" key, any typed numbers are still on the screen, but have not been processed. How to handle that situation? – BryanT Apr 19 '16 at 13:53
  • 1
    @Simas `android:singleLine="true"` it's deprecated and it doesn't work with `maxLines=1`. Any ideas? – Borja Oct 21 '16 at 09:03
  • 1
    @Borja `singleLine` has been replaced by `maxLines=1`. Should work as intended. Create a separate question if it doesn't. – Simas Oct 21 '16 at 14:55
  • @Borja I needed to add `android:inputType="text"` to my `EditText` for it to work correctly, otherwise it kept going to a new line – Drew Szurko Oct 11 '17 at 16:03
  • nice, clean and perfect. thanks a lot @angelrh – Reza Dec 05 '20 at 08:53
  • 1
    Does not work for me. Literally nothing happens when I click on the outside. No matter what I do the keyboard does not disappear – VanessaF Aug 28 '21 at 14:56
132

This should work.

InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); 
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS); 

Just make sure that this.getCurrentFocus() does not return null, which it would if nothing has focus.

javadroid
  • 1,421
  • 2
  • 19
  • 43
Ryan Alford
  • 7,514
  • 6
  • 42
  • 56
  • getting following error oncreate Cannot make a static reference to the non-static method getSystemService(String) from the type – UMAR-MOBITSOLUTIONS Mar 01 '10 at 07:38
  • 32
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0); working now... – UMAR-MOBITSOLUTIONS Mar 01 '10 at 07:52
  • 3
    You don't need to hide soft keyboard with this method. See @angelrh answer below along with the comments which says to return false in OnEditorActionListener and also to mark your edit text as singleLine=true from xml. – Nilesh Jan 24 '16 at 11:21
  • getCurrentFocus() geting null – android_jain Jun 24 '17 at 05:20
  • @android_jain that's because there isn't anything that has focus. – Ryan Alford Jun 25 '17 at 02:09
28
   mEtNumber.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    // do something, e.g. set your TextView here via .setText()
                    InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                    return true;
                }
                return false;
            }
        });

and in xml

  android:imeOptions="actionDone"
Ankita Chopra
  • 329
  • 4
  • 12
12
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
sumit pandey
  • 1,223
  • 1
  • 15
  • 23
10

I did not see anyone using this method:

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean focused) {
        InputMethodManager keyboard = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (focused)
            keyboard.showSoftInput(editText, 0);
        else
            keyboard.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    }
});

And then just request focus to the editText:

editText.requestFocus();
Ofir
  • 171
  • 1
  • 5
5

Solution included in the EditText action listenner:

public void onCreate(Bundle savedInstanceState) {
    ...
    ...
    edittext = (EditText) findViewById(R.id.EditText01);
    edittext.setOnEditorActionListener(new OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                in.hideSoftInputFromWindow(edittext.getApplicationWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
            }
            return false;
        }
    });
    ...
    ...
}
yohm
  • 452
  • 7
  • 14
3

I found this because my EditText wasn't automatically getting dismissed on enter.

This was my original code.

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if ( (actionId == EditorInfo.IME_ACTION_DONE) || ((event.getKeyCode() == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN ))) {

            // Do stuff when user presses enter

            return true;

        }

        return false;
    }
});

I solved it by removing the line

return true;

after doing stuff when user presses enter.

Hope this helps someone.

IsaiahJ
  • 454
  • 7
  • 19
3

Just write down these two lines of code where enter option will work.

InputMethodManager inputManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
Pang
  • 9,564
  • 146
  • 81
  • 122
Shahadat Hossain
  • 533
  • 7
  • 20
2

You can see marked answer on top. But i used getDialog().getCurrentFocus() and working well. I post this answer cause i cant type "this" in my oncreatedialog.

So this is my answer. If you tried marked answer and not worked , you can simply try this:

InputMethodManager inputManager = (InputMethodManager) getActivity().getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getDialog().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
Pang
  • 9,564
  • 146
  • 81
  • 122
Yasin Kaçmaz
  • 6,573
  • 5
  • 40
  • 58
2

you can create a singleton class for call easily like this:

public class KeyboardUtils {

    private static KeyboardUtils instance;
    private InputMethodManager inputMethodManager;

    private KeyboardUtils() {
    }

    public static KeyboardUtils getInstance() {
        if (instance == null)
            instance = new KeyboardUtils();
        return instance;
    }

    private InputMethodManager getInputMethodManager() {
        if (inputMethodManager == null)
            inputMethodManager = (InputMethodManager) Application.getInstance().getSystemService(Activity.INPUT_METHOD_SERVICE);
        return inputMethodManager;
    }

    @SuppressWarnings("ConstantConditions")
    public void hide(final Activity activity) {
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                try {
                    getInputMethodManager().hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

so, after can call in the activity how the next form:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);
        KeyboardUtils.getInstance().hide(this);
    }

}
Alex Zaraos
  • 6,443
  • 2
  • 26
  • 21
2

Been struggling with this for the past days and found a solution that works really well. The soft keyboard is hidden when a touch is done anywhere outside the EditText.

Code posted here: hide default keyboard on click in android

Community
  • 1
  • 1
Daniel
  • 613
  • 1
  • 5
  • 7
1

I use this method to remove keyboard from edit text:

 public static void hideKeyboard(Activity activity, IBinder binder) {
    if (activity != null) {
        InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (binder != null && inputManager != null) {
            inputManager.hideSoftInputFromWindow(binder, 0);//HIDE_NOT_ALWAYS
            inputManager.showSoftInputFromInputMethod(binder, 0);
        }
    }
}

And this method to remove keyboard from activity (not work in some cases - for example, when edittext, to wich is binded keyboard, lost focus, it won't work. But for other situations, it works great, and you do not have to care about element that holds the keyboard).

 public static void hideKeyboard(Activity activity) {
    if (activity != null) {
        InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (activity.getCurrentFocus() != null && inputManager != null) {
            inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
            inputManager.showSoftInputFromInputMethod(activity.getCurrentFocus().getWindowToken(), 0);
        }
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Anton Kizema
  • 1,072
  • 3
  • 13
  • 27
1
int klavStat = 1; // for keyboard soft/hide button
int inType;  // to remeber your default keybort Type

editor - is EditText field

/// metod for onclick button ///
public void keyboard(View view) {
    if (klavStat == 1) {
        klavStat = 0;
        
        inType = editor.getInputType();
        
        InputMethodManager imm = (InputMethodManager)
                getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
     

        editor.setInputType(InputType.TYPE_NULL);

        editor.setTextIsSelectable(true);
    } else {
        klavStat = 1;


        InputMethodManager imm = (InputMethodManager)
                getSystemService(Context.INPUT_METHOD_SERVICE);

        imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);

        editor.setInputType(inType);
    }
}

If you have another EditText Field, you need to watch for focus change.

Pang
  • 9,564
  • 146
  • 81
  • 122
D.Dzugo
  • 13
  • 2
  • For Kit-kat to hide i use this: ` editor.setInputType(InputType.TYPE_NULL); editor.setSingleLine(false); editor.setTextIsSelectable(true);` – D.Dzugo Aug 18 '16 at 19:29
0

In my case, in order to hide the keyboard when pressing the "send button", I used the accepted answer but changed context to getApplication and added getWindow().

InputMethodManager inputManager = (InputMethodManager) getApplication().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
Pang
  • 9,564
  • 146
  • 81
  • 122
Leo S
  • 159
  • 9
-2
editText.setInputType(InputType.TYPE_NULL);
Lamorak
  • 10,957
  • 9
  • 43
  • 57
  • 1
    Please add explanation. Your answer is currently being voted for deletion. Please do so to avoid future misunderstandings. – Daniel Cheung Sep 02 '15 at 12:34
  • @daniel-cheung: I don't understand this kind of comment. Isn't it obvious from the context? If not, this is not the place to be learning the context. – Protean Apr 25 '18 at 11:58
  • 3
    The reason this answer should be down-voted / deleted is that it may look like it works at first, but then the keyboard never shows up again, so it is hardly a proper solution. – Protean Apr 25 '18 at 11:58