1

So i have a custom alert dialog with an EditText. Whenever i click a button to confirm, or if i click the soft keyboard's own done-button, i have programmed the app to close the dialog. However for some strange reason the soft-keyboard is still open after the alert dialog is closed...

This piece of code at the end of buttonConfirm is what i tried to solve this issue. For some reason the code don't work for the button itself but the code does work for the done-button in the soft keyboard

buttonConfirm.setOnClickListener(new 

    View.OnClickListener()
    {..............
    .................
         closeKeyboard();
         Handler handler = new Handler();
         handler.postDelayed(new Runnable() {

         @Override
         public void run() {
             dialog.dismiss();

         }

     }, 100); // 5000ms delay


}

//This is the code for the done-button in the `soft keyboard`

textinputEdit.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event){
         if(actionId==EditorInfo.IME_ACTION_DONE){
              buttonConfirm.performClick();
         }
         return false;
       }
});

So why does that work but not the button itself when directly pressing on it? This is very strange to me.. Anyone know what the hell is going on? :(

alireza easazade
  • 1,577
  • 4
  • 16
  • 27

2 Answers2

1

On clicking the done button, call hideSoftInputFromWindow method -

InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
Prachi Singh
  • 544
  • 2
  • 7
Anupam
  • 2,523
  • 2
  • 12
  • 28
  • Thanks, I found this and it seems to work for me. :) "InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE); getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);" – Daniel Andersson Jun 28 '19 at 16:43
  • Great. Happy coding!! – Anupam Jun 28 '19 at 16:58
0
 public void hideSoftKeyboard(Context context, View view) {
        try {
            InputMethodManager inputMethodManager =
                    (InputMethodManager) context.getSystemService(
                            Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(
                    view.getWindowToken(), 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Usage

textinputEdit.setOnEditorActionListener(new OnEditorActionListener() {
                    @Override
                    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if(actionId==EditorInfo.IME_ACTION_DONE){
                        buttonConfirm.performClick();
                        hideSoftKeyboard(YourActivityName.this,textinputEdit);
                    }
                    return false;
                }
            });
Ganesh Pokale
  • 1,503
  • 10
  • 23