0

I am working on an android application. In an activity at the bottom of the page, I am showing a popupWindow and replaced the keyboard with this popupwindow. This popupWindow is having a searchview inside it, so when searchview will be in focus the keyboard will be shown and popupWindow will be slide up. Now when I press Back Button (Phone's Back Button) then keyboard as well as popupwindow both are getting closed. So, I want to close only keyboard here and popupwindow should be slide down after keyboard will be closed.

I tried the below things :

setBackgroundDrawable(new BitmapDrawable());
poupupWindow.setFocusable(true);

Applied KeyListner also on popupWindow, but it's not working.

     popupWindow.getContentView().setOnKeyListener(new View.OnKeyListener() {
                    @Override
                    public boolean onKey(View v, int keyCode, KeyEvent event) {

                   if (keyCode ==  KeyEvent.KEYCODE_BACK &&
                                event.getAction() == KeyEvent.ACTION_DOWN)              {
                   return true;                        
} return false;
                   }
                });

Please help me if anyone have idea about this. Thanks a ton in advanced :)

Prithniraj Nicyone
  • 5,021
  • 13
  • 52
  • 78

1 Answers1

0

add code to hide keyboard inside key listener using onKeyDown() mehod.

        boolean isKeyboardHidden=false;
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
         if (event.getAction() == KeyEvent.ACTION_DOWN) {
           switch (event.getKeyCode()) {
                case  KeyEvent.KEYCODE_BACK;
                       if(!isKeyboardHidden && popupview.isFocused())                                
                         {
                          InputMethodManager imm = 
                                   (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

                          imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

                          isKeyboardHidden=true;
                         }
                return true;                        
          }
         }
        return super.onKeyDown(keyCode, event);
        }
Ajith Pandian
  • 1,332
  • 13
  • 21
  • But setOnKeyListener is not being called when I press back button when keyboard is opened with popupwindow. How can I get this Listener calling. Please help, Thanks a lot in advanced. :) – Prithniraj Nicyone Sep 15 '16 at 10:29
  • add key listener to your activity or fragment. and check search view is focused or not. if it is focused hide the keyboard. – Ajith Pandian Sep 15 '16 at 10:33
  • Can you please post an example for doing the same. – Prithniraj Nicyone Sep 15 '16 at 10:34
  • I've tried onKeydown method in activity, but it is being called only when popupupWindow is dismissed. I want to detect the back press key when popupwindow will be opened. It would be a great help if you can help me here. :) – Prithniraj Nicyone Sep 15 '16 at 10:49
  • you can also override onBackPressed() method in Activity to hide the keyboard. – Ajith Pandian Sep 15 '16 at 11:17
  • I've tried it. Actually my popupWindow contain Searchview, so when I click on this searchview I set `PopupWindow.setFocusable(true);` and requestFocus to Searchview which shows the keyboard to write something in searchview. Now when I press back button when keyboard and popupwindow is opened, the keyDown/onBeckPressed event is not being called. Please help if you have idea here. Thanks a lot for all your help :) – Prithniraj Nicyone Sep 15 '16 at 11:26
  • Try this link, I think it will help you a lot.[link](http://stackoverflow.com/questions/3940127/intercept-back-button-from-soft-keyboard) – Ajith Pandian Sep 15 '16 at 11:35