Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keyboard is not being hidden when AlertDialog is dismissed

Tags:

android

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();
            }
        });
    }
like image 369
Violet Giraffe Avatar asked Sep 20 '12 13:09

Violet Giraffe


1 Answers

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) {}
like image 65
Yogesh Somani Avatar answered Oct 16 '22 08:10

Yogesh Somani