6

I have extended AlertDialog with my class that displays my XML layout. I don't use AlertDialog's standard buttons, I have my own OK and Cancel buttons. Listener for them calls dismiss(). The problem is if I was editing EditText's contents and then pressed OK (it's an Android 3.1 tablet, keyboard doesn't prevent me from interacting with the dialog), the dialog will hide but keyboard won't, it'll stay in background. What could be the reason and how to fix it?

Here's a constructor of my dialog, to give the idea:

public NetworkCameraParametersDialog(Context context ) {
        super(context);

        View content = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.dialog, null);
        setView(content);

        Button btnOk = (Button) content.findViewById(R.id.btn_Ok);
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                                // Some work
                dismiss();              
            }
        });

        Button btnClose = (Button) content.findViewById(R.id.btn_Close);
        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }
Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
  • Can this exist a other editable object that get focus on `dismiss()`? Also a code dump can be to help here. – FIG-GHD742 Sep 20 '12 at 13:13
  • @FIG-GHD742: Another - as in not from this dialog? No. – Violet Giraffe Sep 20 '12 at 13:15
  • you can force the keyboard to go away, http://stackoverflow.com/q/7200281/995891 – zapl Sep 20 '12 at 13:16
  • @zapl: I'll try it, but I don't like the idea. I have many dialogs of the same structure, and none of them suffers this problem. – Violet Giraffe Sep 20 '12 at 13:20
  • 1
    The keyboard sometimes behaves strange when `EditText`s are present. I have not found a good solution to prevent it from showing / hiding in undesired ways in some cases but to manually force it to show / hide. Would love to understand how to fix this – zapl Sep 20 '12 at 13:28
  • The solution you've posted does indeed work, but, like I said, I have at least 5 other dialogs with `EditText`s, and they're all fine. Weird. – Violet Giraffe Sep 20 '12 at 13:29

1 Answers1

4

You can force soft keyboard to hide:

    try {
        InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
    } catch (Exception e) {}
Yogesh Somani
  • 2,624
  • 3
  • 21
  • 34
  • yep, that's what I did, for every TextView. But real question is - what's causing the problem. P. S. Is `try`/`catch` necessary here? – Violet Giraffe Sep 20 '12 at 16:27
  • I think thats the default behaviour of keyboard because I was facing the same proble when I was creating an app in which there was a login page. I wanted that as soon as user clicks on Login button after filling the username and password in the EditTexts , keyboard should immediately hide. But it was not happening , so I had to do the above. As far as try-catch is concerned , I am not sure about it. – Yogesh Somani Sep 21 '12 at 04:37
  • 1
    Please avoid giving response with generic `try..catch`. – Thierry J. Oct 09 '14 at 03:35
  • Well this try catch is actually pretty wise... Otherwise you end up with something like this: InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); if( inputManager!=null&&this.getCurrentFocus()!=null) inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0); – Renetik Jan 18 '17 at 19:04