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(); } 
4
  • Do you mean the following didn't work for you: InputMethodManager imm = (InputMethodManager)getSystemService( Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0);? Commented Nov 24, 2013 at 7:52
  • @omega: Are you frocibly bringing the keyboard up.? Commented Nov 24, 2013 at 7:53
  • The keyboard comes up by focusing on the edittext, so no I don't forcibly bring it up. Commented Nov 24, 2013 at 7:55
  • I added the code above. Commented Nov 24, 2013 at 8:00

1 Answer 1

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); 
Sign up to request clarification or add additional context in comments.

2 Comments

What is handler and where do I get it from?
@omega: I have added handler code. Just paste it in your code and replace editText with your name and try.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.