The soft keyboard does not hide programmatically in android

I am new to android and working on a demo alert dialog, I want to close the soft keyboard when one of the buttons from the warning is pressed. I tried this programmatically, but the keyboard remains open, can you help me for this question, code

public void Show_Dialog() { final AlertDialog.Builder alertDialog = new AlertDialog.Builder( SwipeActivity.this); LayoutInflater inflater = this.getLayoutInflater(); final View layout = inflater.inflate(R.layout.add_albom_dialog, null); alertDialog.setView(layout); final InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); //android:digits="abcdefghijklmnopqrstuvwxyz1234567890 " alertDialog.setPositiveButton("Create", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { EditText txts = (EditText) layout .findViewById(R.id.addAblum_edit); hideSoftKeyboardDialogDismiss(SwipeActivity.this); if(txts.getText().toString().trim().length() > 0) { Add_album(txts.getText().toString()); } else { AlertDialog alertDialog = new AlertDialog.Builder(SwipeActivity.this).create(); alertDialog.setTitle("Error"); alertDialog.setMessage("Name can't be emtpy"); alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); inputManager.hideSoftInputFromInputMethod(getCurrentFocus().getWindowToken(), 0); } }); alertDialog.show(); } dialog.cancel(); // Your custom code } }); /* When negative (No/cancel) button is clicked */ alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { hideSoftKeyboardDialogDismiss(SwipeActivity.this); dialog.cancel(); // finish(); } }); alertDialog.show(); } 
+5
source share
4 answers

Try the following:

 protected void hideSoftKeyboard(EditText mSearchView) { InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); mgr.hideSoftInputFromWindow(mSearchView.getWindowToken(), 0); } 
+3
source
 dialog.setOnDissmissListener(){ void onDismiss(){ inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); inputManager.hideSoftInputFromInputMethod(getCurrentFocus().getWindowToken(), 0); } } dialog.dismiss(); 
+2
source

Try this as follows.

 final InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); final AlertDialog alertDialog = new AlertDialog.Builder(SwipeActivity.this).create(); alertDialog.setTitle("Error"); alertDialog.setMessage("Name can't be emtpy"); alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { inputManager.hideSoftInputFromInputMethod(alertDialog.getCurrentFocus().getWindowToken(), 0); dialog.dismiss(); } }); alertDialog.show(); 

Use alertDailog current focus, not your activity.

+1
source

There really should be a delay, so use this code

  public static void hideSoftKeyboardDialogDismiss(final Activity activity) { new Handler().postDelayed(new Runnable() { @Override public void run() { activity.runOnUiThread(new Runnable() { @Override public void run() { InputMethodManager inputMethodManager = (InputMethodManager) activity .getSystemService(Activity.INPUT_METHOD_SERVICE); if (null != activity.getCurrentFocus()) { inputMethodManager.hideSoftInputFromWindow(activity .getCurrentFocus().getWindowToken(), 0); } } }); } }, 1); } 
+1
source

Source: https://habr.com/ru/post/1244815/


All Articles