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.
To hide keyboard, use the following code. InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); inputMethodManager. hideSoftInputFromWindow(v. getApplicationWindowToken(),0);
Tap the back button on your Android. It's the left-pointing arrow button at the bottom of the screen, either at the bottom-left or bottom-right corner. The keyboard is now hidden. The back button may be a physical button or on the touch screen.
and put the following code in the onTouch method. InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm. hideSoftInputFromWindow(getCurrentFocus(). getWindowToken(), 0);
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); 
                        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 } 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With