Android:Hide keyboard after button click

75,075

Solution 1

You could instead set it to your layout, ie:

LinearLayout mainLayout;

// Get your layout set up, this is just an example
mainLayout = (LinearLayout)findViewById(R.id.myLinearLayout);

// Then just use the following:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mainLayout.getWindowToken(), 0);

This is an example assuming that your layout will be created regardless of how many EditText objects (or other objects) are placed on it.

Edit: Also, something I find very useful is to make sure that the keyboard is hidden when an activity first launches (ie: if an EditText is the first thing focused). To do that, I put this in onCreate() method of Activity:

 this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

Solution 2

Dont forget to use try catch blog because in case when your keyboard not open and if you use key key board hide code app will crash

try {
    InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
    // TODO: handle exception
}

Solution 3

You can hide the keyboard using the following code, probably on the Button click Event :

//================ Hide Virtual Key Board When  Clicking==================//

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow("Your Button/EditText Object".getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);

//======== Hide Virtual Keyboard =====================//

Solution 4

If the problem is on an activity simply the following will work:

    try {
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    } catch (Exception e) {
        // TODO: handle exception
    }

else if the code is required in a fragment do the following

    try {
        InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
    } catch (Exception e) {
        // TODO: handle exception
    }

This will handle the hiding of keyboard on a button click or any other event as deemed specific when written within the event block.

Solution 5

edittext.onEditorAction(EditorInfo.IME_ACTION_DONE);
Share:
75,075
Martin Shinks
Author by

Martin Shinks

Updated on November 18, 2020

Comments

  • Martin Shinks
    Martin Shinks over 3 years

    I need to hide the android keyboard after a button click.

    I have seen many examples of how to do this, however, they all appear to use a specific editText object.

    e.g.

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

    My problem is that I am building the screen dynamically, thus there could be mane edit text fields. Is there a way the keyboard can be hidden without me having to specify which editText object I am hiding it for.

  • Michael
    Michael over 10 years
    Note that it also works for other view parts, e.g. an EditText :) Then you would change the following line: imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
  • Kumar Saurabh
    Kumar Saurabh almost 7 years
    by far the most simple and elegant solution
  • Alex Escobar
    Alex Escobar over 3 years
    Most ellegant, generic and simple solution, i love it
  • DennisVA
    DennisVA about 3 years
    Underrated answer
  • tiktak
    tiktak about 2 years
    did not work for me, whereas using the layout to fetch the windowToken did
  • milad salimi
    milad salimi almost 2 years
    What did u mean about windowToken?!