_:ne295112b521d4c25a94fa435bed63201xb5
<http://schema.org/text>
"To help clarify this madness, I&#x27;d like to begin by apologizing on behalf of all Android users for Google&#x27;s downright ridiculous treatment of the soft keyboard. The reason there are so many answers, each different, for the same simple question is that this API, like many others in Android, is horribly designed. I can think of no polite way to state it.\nI want to hide the keyboard.  I expect to provide Android with the following statement: Keyboard.hide(). The end. Thank you very much.  But Android has a problem.  You must use the InputMethodManager to hide the keyboard. OK, fine, this is Android&#x27;s API to the keyboard. BUT! You are required to have a Context in order to get access to the IMM. Now we have a problem.  I may want to hide the keyboard from a static or utility class that has no use or need for any Context. or And FAR worse, the IMM requires that you specify what View (or even worse, what Window) you want to hide the keyboard FROM.\nThis is what makes hiding the keyboard so challenging. Dear Google: When I&#x27;m looking up the recipe for a cake, there is no RecipeProvider on Earth that would refuse to provide me with the recipe unless I first answer WHO the cake will be eaten by AND where it will be eaten!!\nThis sad story ends with the ugly truth: to hide the Android keyboard, you will be required to provide 2 forms of identification: a Context and either a View or a Window.\nI have created a static utility method that can do the job VERY solidly, provided you call it from an Activity.\npublic static void hideKeyboard(Activity activity) {\n    InputMethodManager imm &#x3D; (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);\n    //Find the currently focused view, so we can grab the correct window token from it.\n    View view &#x3D; activity.getCurrentFocus();\n    //If no view currently has focus, create a new one, just so we can grab a window token from it\n    if (view &#x3D;&#x3D; null) {\n        view &#x3D; new View(activity);\n    }\n    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);\n}\n\nBe aware that this utility method ONLY works when called from an Activity! The above method calls getCurrentFocus of the target Activity to fetch the proper window token.\nBut suppose you want to hide the keyboard from an EditText hosted in a DialogFragment? You can&#x27;t use the method above for that:\nhideKeyboard(getActivity()); //won&#x27;t work\n\nThis won&#x27;t work because you&#x27;ll be passing a reference to the Fragment&#x27;s host Activity, which will have no focused control while the Fragment is shown! Wow! So, for hiding the keyboard from fragments, I resort to the lower-level, more common, and uglier:\npublic static void hideKeyboardFrom(Context context, View view) {\n    InputMethodManager imm &#x3D; (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);\n    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);\n}\n\nBelow is some additional information gleaned from more time wasted chasing this solution:\nAbout windowSoftInputMode\nThere&#x27;s yet another point of contention to be aware of. By default, Android will automatically assign initial focus to the first EditText or focusable control in your Activity.  It naturally follows that the InputMethod (typically the soft keyboard) will respond to the focus event by showing itself. The windowSoftInputMode attribute in AndroidManifest.xml, when set to stateAlwaysHidden, instructs the keyboard to ignore this automatically-assigned initial focus.\n&amp;lt;activity\n    android:name&#x3D;&amp;quot;.MyActivity&amp;quot;\n    android:windowSoftInputMode&#x3D;&amp;quot;stateAlwaysHidden&amp;quot;/&amp;gt;\n\nAlmost unbelievably, it appears to do nothing to prevent the keyboard from opening when you touch the control (unless focusable&#x3D;&amp;quot;false&amp;quot; and/or focusableInTouchMode&#x3D;&amp;quot;false&amp;quot; are assigned to the control). Apparently, the windowSoftInputMode setting applies only to automatic focus events, not to focus events triggered by touch events.\nTherefore, stateAlwaysHidden is VERY poorly named indeed. It should perhaps be called ignoreInitialFocus instead.\n\nUPDATE: More ways to get a window token\nIf there is no focused view (e.g. can happen if you just changed fragments), there are other views that will supply a useful window token.\nThese are alternatives for the above code if (view &#x3D;&#x3D; null)   view &#x3D; new View(activity);  These don&#x27;t refer explicitly to your activity.\nInside a fragment class:\nview &#x3D; getView().getRootView().getWindowToken();\n\nGiven a fragment fragment as a parameter:\nview &#x3D; fragment.getView().getRootView().getWindowToken();\n\nStarting from your content body:\nview &#x3D; findViewById(android.R.id.content).getRootView().getWindowToken();\n\n\nUPDATE 2: Clear focus to avoid showing keyboard again if you open the app from the background\nAdd this line to the end of the method:\nview.clearFocus();"
<https://find-error.com/questions/1109022/how-do-you-close-hide-the-android-soft-keyboard-programmatically>