2

Android soft keyboard hiding Date picker dialog

As shown in the picture, I have a date picker dialog which is launched on click of an Edittext control. But it is partially hidden behind the soft keyboard which was already open from previous Edittext control. How do I hide the soft keyboard when I launch the date picker dialog?

I have already tried the code from 1 and 2, but doesn't help.

Community
  • 1
  • 1
bighi
  • 247
  • 3
  • 12
  • The answer in the first link worked for me. Please post your code, you might be doing something wrong. – the-ginger-geek Nov 11 '15 at 03:57
  • change the edit text to a textview. why do you have to load the calendar on edit text click? – TharakaNirmana Nov 11 '15 at 05:12
  • Actually I have a date field (edit text). On click of that field should pop up a date picker dialog for the user to select a date. I am not sure if this is the best way to do. btw, how does it help by changing the field to a textview? – bighi Nov 11 '15 at 15:45
  • Figured it out what the issue was. Since there were two places where the date picker dialog was , I had to hide the soft keyboard in both places. Thanks all for the suggestions. – bighi Nov 12 '15 at 01:32

1 Answers1

1

You can check on onClick event, if the keyboard is open. If its open you can forcibly close it.

View view = this.getCurrentFocus();
if (view != null) {  
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

if this does not help you can also try the below code: You should check for onFocus change event on the the view

View.OnFocusChangeListener listener;
    listener = new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (v.getId() == R.id.address && !hasFocus) {
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                    }

                }
            };
Sam
  • 452
  • 5
  • 15