1

I have a dialog box open in my android app, and I have a button when clicked will dismiss the dialog box. The problem is there is also a textedit field, and if its focused and the keyboard is showing, then when I click the cancel button, then dialog goes away, but the keyboard is still showing.

I want to also dismiss the keyboard.

I was searching around, and for threads like this Hide soft keyboard after dialog dismiss

But none of the solutions worked for me. By the way the edittext is a number input type, if that makes a difference somehow.

Does anyone know how to fix this?

Thanks

public void HandleTeamManagement() {
    final Dialog teamDialog = new Dialog(this);
    teamDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    teamDialog.setContentView(R.layout.dialog_team_management);

    final EditText mergeNum = (EditText) teamDialog.findViewById(R.id.group);


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



    // Setting Negative "NO" Button
    Button cancelButton = (Button) teamDialog.findViewById(R.id.cancel);
    cancelButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            teamDialog.dismiss();
        }
    });

    // Showing Alert Dialog
    teamDialog.show();
}
Community
  • 1
  • 1
omega
  • 40,311
  • 81
  • 251
  • 474

1 Answers1

3

You can find a solution here:

http://www.workingfromhere.com/blog/2011/04/27/close-hide-the-android-soft-keyboard/

Close/hide the Android Soft Keyboard

Edited: adding code Try this ..it worked for me

            Handler handler = new Handler();
    handler.postDelayed(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            if (editText!= null && getActivity() != null) {
                InputMethodManager imm = (InputMethodManager) getActivity()
                        .getSystemService(
                                Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(
                        editText.getWindowToken(), 0);
            }
        }
    }, 1000);
Community
  • 1
  • 1
Vaibs
  • 1,128
  • 3
  • 16
  • 36