Razvi
Razvi

Reputation: 398

How can I get back button working when searchview in Android

I've been searching for more than 4 hours all over the internet how to get the back button working properly when searchview is opened but I just can't achieve it.

Here is my searchview.

    searchview  = (SearchView) findViewById(R.id.searchView);
        searchview.setOnClickListener(this);
  @Override
    public void onClick(View v) {
        if (v.getId() == R.id.searchView){

           //Cursor cursor = manager.obtenerIDColumnaServicio(searchview.getQuery().toString());
            adapterOfDataBase.changeCursor(cursor);
        }
    }

Here is the xml:

   <SearchView
                    android:layout_width="match_parent"
                    android:layout_height="63dp"
                    android:id="@+id/searchView"
                    android:iconifiedByDefault="true"
                    android:backgroundTint="#ff000000"
                    android:layout_marginRight="0dp" />

All this withing a huge class using SQL and some methods to insert, delete, etc etc tables & entries. So my problem is that everytime I click on the searchview it expands in order to write the text, everyting ok, but when I push back button from android nothing happens.

I tried 2 options but none worked, if you could just help me....

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) { //MEJORAR ESTO
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        searchview.onActionViewCollapsed();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
//None of this 2 methods are working. I tried them separately
@Override
public void onBackPressed(){
    if (!this.searchview.isIconified()) {
        this.searchview.setIconified(true);
    } else {
        super.onBackPressed();
    }
}

Upvotes: 4

Views: 7652

Answers (5)

user12844185
user12844185

Reputation:

Starting at Android 9, Google fixes this problem by themselves.

Upvotes: 0

topcbl
topcbl

Reputation: 849

Try to use OnActionExpandListener on MenuItem

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_search, menu);
        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        MenuItem searchItem = menu.findItem(R.id.search);
        SearchView searchView = null;
        if (searchItem != null) {
            searchView = (SearchView) searchItem.getActionView();
        }
        if (searchView != null) {
            searchView.setSearchableInfo(searchManager.getSearchableInfo(MainActivity.this.getComponentName()));
        }
        searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                return true;
            }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            // TODO do your stuff when back button is pressed
            return true;
        }
    });
    return super.onCreateOptionsMenu(menu);
}

In case of you use AppCompatSupportv7

MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                return true;
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                // TODO do your stuff when back button is pressed
                return true;
            }
        });

Upvotes: 2

fpanizza
fpanizza

Reputation: 1589

In my case just adding :

setFocusableInTouchMode(true);

to the SearchView works as a work around for this issue,

Hope it helps,

Upvotes: 4

gemini
gemini

Reputation: 369

When searchView is open and back button is pressed you expect to see the searchView closed (aka setIconified(true)) This is a standar behaviour as per google apps searchview implementation.

So all you need to do is something like that

    mSearchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                mSearchView.setIconified(true);
            }
        }
    });

Upvotes: 11

Giuseppe
Giuseppe

Reputation: 2103

instead of this.searchview.setIconified(true) try this if you are using support library:

searchview.onActionViewCollapsed();
supportInvalidateOptionsMenu();

or this if you are not using support library:

searchview.onActionViewCollapsed();
invalidateOptionsMenu();

Upvotes: 0

Related Questions