0

I've spent most of the day searching and trying various solutions and while I've come most of the way, I'm cursed by the "close" (maybe it has more appropriate name, idk) button on the nav bar when the keyboard is displayed (as in the attached image.)

I have a few editTexts which allow the user to adjust some parameters before a graph is recomputed and redrawn. I need to know when their input is complete. I've managed to sort out the "Done" button but for the life of me I can't figure out how to handle hitting that close button. I've also adapted some code which determines if the keyboard was opened and then closed (which is the case with these editTexts) but it only works when using the Done button (so is somewhat redundant).

So.. is there some way of picking up when the user has closed the keyboard using the nav bar?

TIA

*@! keyboard

TrustNoOne
  • 585
  • 1
  • 5
  • 15

1 Answers1

0

Well, I think I have something here, perhaps it will be useful to others. Who knows how long it will work properly.

So prelude: I needed to be able to tell when a user had completed changing an edit text so that I could then redo a computation and update a graph. I really have no room for an "update" button and felt from a UI standpoint it would have quickly become annoying.

Codewise:

I have a "display" method with an inner "watcher" class with a watch() method. The watcher class has many things, including the various editTexts which watch()..watches. I ddapted some code I found here, which determines if the keyboard is open or closed. currentView is the root view of the graphing activity.

            currentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

                @Override
                public void onGlobalLayout() {
                    // TODO Auto-generated method stub
                    Rect r = new Rect();
                    getWindow().getDecorView().getWindowVisibleDisplayFrame(r);

                    int screenHeight = getWindow().getDecorView().getRootView().getHeight();
                    int heightDifference = screenHeight - (r.bottom - r.top);
                    if (MainActivity.debug) Log.d("Keyboard Size", "Size: " + heightDifference+" screenHeight: "+screenHeight);
                    boolean keyboardVisible = heightDifference > (screenHeight / 3);
                    if (keyboardToggle) { // someone has updated one of the editTexts
                        if(!keyboardVisible) { // and they have closed the soft keyboard so are done
                            // do something
                            if(MainActivity.debug) {
                                if(rIDchangedET == firstET.getId()) Log.i(TAG,"update the first thing");
                                if(rIDchangedET == secondET.getId()) Log.i(TAG,"update the second thing");
                                if(rIDchangedET == thirdET.getId()) Log.i(TAG,"update the third thing");
                            }
                            // reset the keyboard toggle
                            keyboardToggle = false;
                        }
                    }
                }
            });

This helps but in a somewhat backward way. Knowing open/closed by itself is not useful within any of the text watchers as those are not triggered by the act of closing the keyboard (so you can't actually test for it within the listener)

However, thinking about it a bit I added a boolean keyboard toggle which is set to true in the afterTextChanged of the addTextChangedListener. In addition, I put the rID of that editText in another class variable.

The code which checks the keyboard status then calls the appropriate updating procedure if the a) keyboard is closed b) some text has actually been changed (keyboard toggle) and c) uses the rID to determine what procedure to then call.

I know its a bit convoluted, but it does seem to work.

Community
  • 1
  • 1
TrustNoOne
  • 585
  • 1
  • 5
  • 15