0

I have an app that uses the keyboard, when the keyboard is up if the user presses back button the keyboard dissappears, however I would like to change one or two other things while it changes, so I need an event listener.

I tried

 @Override
public void onBackPressed()

However this doesnt seem to catch the backbutton while the keyboard is up, if the user presses back twice, then this catches the second click only

Adam Katz
  • 5,706
  • 7
  • 36
  • 62
  • Check [this](http://stackoverflow.com/questions/2150078/how-to-check-visibility-of-software-keyboard-in-android?answertab=active#tab-top) out. And next time please do a little research before posting a quesiton. – Abbas Aug 15 '16 at 13:10
  • Not really the same question, my main point is to find out when back button is pressed, not a general catch all for finding whether the keyboard is there or not – Adam Katz Aug 15 '16 at 13:14
  • As far as I am aware you cannot do that. However there might be a workaround for a limited number of `EditText`s. Check [onKeyPreIme](https://developer.android.com/reference/android/widget/TextView.html#onKeyPreIme). You can use keycode to compare with `KeyEvent.KeyCode_Back`. But how are you going to get all the events of all the `EditText`s to the `MainActiivty` is beyond me. – Abbas Aug 15 '16 at 13:25
  • So there is no way to simple fire off an event of backbutton is pressed when keyboard is up? – Adam Katz Aug 15 '16 at 13:28
  • But you could call your `MainActivity.onBackPressed()` from all the events. – Abbas Aug 15 '16 at 13:29
  • No direct way at least. But you could gather all the events (as suggested earlier) in `onBackPressed()` of your `MainActivity` and handle it there. – Abbas Aug 15 '16 at 13:30

1 Answers1

0

Register this on your root view to detect keyboard appearance/disappearance :

root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
 public void onGlobalLayout(){
       int heightDiff = root.getRootView().getHeight()- root.getHeight();
       /*If height difference is more then 150, consider keyboard as visible. -150 for disappearing */ 

    }

});

Also, make sure to use adjustResize for keyboard.