0

In my scenario I have an EditText that gains the focus. When I close the keyboard using the back button, I would like to catch that event and do some stuff like give the focus to another view.

How do I get the keyboard onclose event?

Anthea
  • 3,451
  • 4
  • 37
  • 60

2 Answers2

1

there is cabback for softkeyboard. I have used this code snippet, hnestly i have forgot from where :(

final View activityRootView = findViewById(R.id.loging_rootview);
        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
                new OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        int heightDiff = activityRootView.getRootView()
                                .getHeight()
                            - activityRootView.getHeight();
                        if (heightDiff > 138) { // if more than 100 pixels, its
                            // probably a keyboard...

                            logo.setVisibility(View.GONE);
                        } else {

                            logo.setVisibility(View.VISIBLE);
                        }
                    }
                });
Anis BEN NSIR
  • 2,537
  • 18
  • 28
1

You can get return button as

editText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// You can identify which key pressed buy checking keyCode value
// with KeyEvent.KEYCODE

   if (keyCode == KeyEvent.KEYCODE_ENTER && KeyEvent.ACTION_DOWN == vent.getAction())     {
          // YOUR CODE HERE
   }

   return false;
   }
});

EDIT :

There is no events occurs when soft keyboard hides. see this Question will help you to solve your problem

Community
  • 1
  • 1
MAC
  • 15,363
  • 8
  • 51
  • 92