2

The user presses the hide keyboard button or the back button. So I need to clear focus on the SearchView when the user is hiding the keyboard.

I tried this but it's not working. focus remains when the user hides the keyboard.

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                searchView.clearFocus();
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                app.requests.getApi().search(newText).enqueue(SearchFragment.this);
                return false;
            }
        });

and this:

searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    app.functions.logWrite("has focus to searchview");
                } else {
                    //code
                }
            }
        });
David Kroukamp
  • 34,930
  • 13
  • 72
  • 130
Mkurbanov
  • 133
  • 2
  • 10

2 Answers2

1

Okay so try this it needs the use of a library unfortunately but it makes it easier.

In your build.gradle: add this:

dependencies {
    implementation 'net.yslibrary.keyboardvisibilityevent:keyboardvisibilityevent:3.0.0-RC2'
}

Register for the keyboard events using KeyboardVisibilityEvent library like this in the fragment/class where SearchView is declared:

KeyboardVisibilityEvent.setEventListener(
    getActivity(),
    new KeyboardVisibilityEventListener() {
        @Override
        public void onVisibilityChanged(boolean isOpen) {
            if (!isOpen) {
               View focusedView = getWindow().getCurrentFocus();
               if (focusedView != null && focusedView instanceof SearchView) { // does SearchView have focus?
                    searchView.clearFocus();
                }
            }
        }
    });

searchView.clearFocus(); works on the assumption you have another focusable view in the hierarchy, if not add this to your fragments layout:

android:focusableInTouchMode="true"

Alternatively simply call focus(); on any other view element you want to receive focus.

David Kroukamp
  • 34,930
  • 13
  • 72
  • 130
  • sorry bro it's not working. when i press down on keyboard. – Mkurbanov Dec 02 '20 at 06:53
  • Okay but it's working when the searchview is closed via the back button ? And you saw my updated answer right ? – David Kroukamp Dec 02 '20 at 06:54
  • Also it wouldn't make sense to dismiss the search when the keyboard is hidden only if the back buttons are pressed.. I mean imagine the user searches then dismissed the keyboard to see the results in full screen, now the search view is closed not sure that makes sense – David Kroukamp Dec 02 '20 at 06:59
  • David Kroukamp not working bro ( – Mkurbanov Dec 02 '20 at 06:59
  • So it's not working only when the keyboard is hidden? Or what ? Sorry just need more clarification – David Kroukamp Dec 02 '20 at 07:02
  • yes it's not working only when i press down on keyboard. – Mkurbanov Dec 02 '20 at 07:03
  • Did you see my other comment does it really make sense to hide the searchview when the keyboard is simply hidden? I don't think so... – David Kroukamp Dec 02 '20 at 07:04
  • yes it makes sense.. on NETFLIX it's workking.When i press down on keyboard netlifx's searcView automaticly clear focus – Mkurbanov Dec 02 '20 at 07:07
  • @Mkurbanov see my updated answer its a bit long but that should get you what you need i.e. removing focus from the searchview when the keyboard is hidden – David Kroukamp Dec 02 '20 at 07:25
  • Updated answer to check `focusedView != null` as this may cause an exception if we dont – David Kroukamp Dec 02 '20 at 07:31
  • 1
    thanks man. it's working. But i tried this without EventBus. And it to working. Why you using EventBus i don't know. can you explain? and thx) – Mkurbanov Dec 02 '20 at 08:08
  • Awesome. You don't need eventbus but I don't know how your app was setup, eventbus just makes life easier to communicate between activities and fragments and vice versa. So for example if you registered the keyboard listener in your activity but the searchview existed in your fragment eventbus makes it easier to communicate to the fragment about the event that's registered in your activity without using intents or localbroadcasts – David Kroukamp Dec 02 '20 at 08:19
  • But maybe it was overkill lol ill add an update. – David Kroukamp Dec 02 '20 at 08:20
0

This is what I use for handling back button clicks for SearchView, by Overriding onBackPressed()

@Override
    public void onBackPressed() {
        if (!searchView.isIconified()) {
            searchView.setIconified(true);
            ANY_VIEW_IN_YOUR_LAYOUT.requestFocus();
        } else
            super.onBackPressed();
    }

Hope this helps. Feel free to ask for clarifications...

Vishnu
  • 525
  • 2
  • 5
  • 18