Search code examples
androidkeyboardinputbox

How can I disable the keyboard when getting out of an input dialog box?


Good morning,

I have a small question regarding the keyboard interraction. Each time I call the input dialog box, regardless I click on OK or Cancel, the keyboard does not desappear. Is there an easy way to achieve that?

Thanks a lot for your help and best wishes, Laurent

            // set dialog message
            alertDialogBuilder
                    .setCancelable(false)
                    .setPositiveButton("OK",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,int id) {
                                    // get user input and set it to result
                                    // edit text
                                    //result.setText(userInput.getText());
                                    edi.setText(userInput.getText());
                                }
                            })
                    .setNegativeButton("Cancel",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,int id) {
                                    dialog.cancel();
                                }
                            });

Solution

  • You can use following code to force hide the keyboard:

    InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 
    imm.hideSoftInputFromWindow(userInput.getWindowToken(), 0);