38

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.

AgentP
  • 6,261
  • 2
  • 31
  • 52
Martin Shinks
  • 545
  • 2
  • 5
  • 10
  • You can hide it for the whole Activity, this sums it up nicely: http://stackoverflow.com/questions/7789514/how-to-get-activitys-windowtoken-without-view – A--C Nov 27 '12 at 21:15

9 Answers9

63

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);
Ashish Vora
  • 571
  • 1
  • 8
  • 27
burmat
  • 2,548
  • 1
  • 23
  • 28
  • 4
    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);` – Michael Dec 05 '13 at 12:39
57

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
}
Ivanka Todorova
  • 9,964
  • 16
  • 66
  • 103
15

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 =====================//
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
9

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.

8
edittext.onEditorAction(EditorInfo.IME_ACTION_DONE);
Makvin
  • 3,475
  • 27
  • 26
4

For the record and based in the answers of @burmat and @Prashant Maheshwari Andro

Let's say that you call the click button as follow. where buttonAndroidLogin_button is the Button object.

protected void onCreate(Bundle savedInstanceState) {
    // your code...
    buttonAndroidLogin_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            hideKeyboard((Button)v);
            // ....
} // end onCreate

On the activity, you must set the next method

public void hideKeyboard(View view) {
    try {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    } catch(Exception ignored) {
    }
}

It hides the input using the same button, so we don't need any linear layout, text view or any other arbitrary control. Also, the code is reusable for more buttons.

magallanes
  • 6,583
  • 4
  • 54
  • 55
3

You use this code

// 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
2

IN KOTLIN :

In your fragment :

Create extension like this :

fun Fragment.hideKeyboard() {
    val imm = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(requireView().windowToken, 0)
}

Then use it like this :

hideKeyboard()

In your activity :

Create extension like this :

fun AppCompatActivity.hideKeyboard() {
    val imm = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(this.window.attributes.token, 0)
}

Then use it like this :

   hideKeyboard()
milad salimi
  • 1,580
  • 2
  • 12
  • 31
0
InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(rootView.getWindowToken(), 0);
Cocoatype
  • 2,572
  • 25
  • 42