1public void hideKeyboard(Context context, View view) {
2 InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
3 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
4}
5
6// call from inside an Activity...
7hideKeyboard(this, view);
8hideKeyboard(this, getCurrentFocus());
9hideKeyboard(this, getWindow().getDecorView());
10hideKeyboard(this, findViewById(android.R.id.content));
1public static void hideKeyboard(Activity activity) {
2 InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
3 //Find the currently focused view, so we can grab the correct window token from it.
4 View view = activity.getCurrentFocus();
5 //If no view currently has focus, create a new one, just so we can grab a window token from it
6 if (view == null) {
7 view = new View(activity);
8 }
9 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
10 }
1
2class CloseHideSoftKeyboard : AppCompatActivity() {
3
4 override fun onCreate(savedInstanceState: Bundle?) {
5 super.onCreate(savedInstanceState)
6 setContentView(R.layout.activity_message)
7
8 val editTextXml = findViewById<EditText>(R.id.editText)
9 val btnSendMessage = findViewById<Button>(R.id.btnSend)
10
11 btnSendMessage.setOnClickListener{
12 // ... you actions
13 // Important! EditText must have be focused
14 // do action close keyboard first before go to another
15 // activity or fragment
16 closeSoftKeyboard(this, editTextXml)
17 }
18 }
19
20
21
22 /* hide soft keyboard after writing and sending message or any */
23 private fun closeSoftKeyboard(context: Context, v: View) {
24 val iMm = context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
25 iMm.hideSoftInputFromWindow(v.windowToken, 0)
26 v.clearFocus()
27 }
28}
29// link to resourse (Russian version)
30// https://issue.life/questions/1109022/close-hide-the-android-soft-keyboard
1//With AndroidX:
2fun View.hideKeyboard() = ViewCompat.getWindowInsetsController(this)
3 ?.hide(WindowInsetsCompat.Type.ime())
1// This is use in Acitivity
2
3InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
4imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
5
6//This is use in Fragement
7InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
8imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
9
10
11Source: StakeOverFlow
1<activity android:name="com.your.package.ActivityName"
2 android:windowSoftInputMode="stateHidden" />