Search code examples
androidsearchviewandroid-appcompat

How to collapse SearchView on backpressed using appcompat v7.?


I am using appcompat v7 for searchview with toolbar except actionbar. below is my menu xml file and java file.

menu file:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto" >

    <item
        android:id="@+id/action_search"
        android:icon="@drawable/ic_action_search"
        android:title="@string/search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always"/>
    <item
        android:id="@+id/action_sort"
        android:icon="@drawable/ic_action_sort"
        android:title="@string/sort"
        app:showAsAction="ifRoom"/>

</menu>

java file:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.dashboard, menu);

         MenuItem searchItem = menu.findItem(R.id.action_search);
        SearchManager searchManager = (SearchManager) MainActivity.this.getSystemService(Context.SEARCH_SERVICE);


        if (searchItem != null) {
            searchView = (SearchView) searchItem.getActionView();
        }
        if (searchView != null) {
            searchView.setSearchableInfo(searchManager.getSearchableInfo(MainActivity.this.getComponentName()));
        }
            return super.onCreateOptionsMenu(menu);
    }

now i want to collapse searchview if it is expanded otherwise want to work backpressed on backpressed() method. how can i achieve this.?


Solution

  • Collapsing on backpressed is handled by default in my own setup. I didn't implement a custom onBackPressed method. I did nothing special except extending from ActionBarActivity.

    I used MenuItemCompat to get actionview, that might do the trick.

    This is my menu xml

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.yourapp.youractivity">
    
    <item
        android:id="@+id/search"
        android:title="@string/app_name"
        android:icon="@drawable/nav_search"
        app:showAsAction="ifRoom|collapseActionView"
        app:actionViewClass="android.support.v7.widget.SearchView" />
    
    </menu>
    

    This is how i create the menu

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    
        getMenuInflater().inflate(R.menu.shop_list, menu);
    
        SearchManager searchManager = 
                      (SearchManager)getSystemService(Context.SEARCH_SERVICE);
    
        //Using MenuItemCompat here, that can do the trick
        searchView = 
                (SearchView)MenuItemCompat.
                            getActionView(menu.findItem(R.id.search));
    
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));
    
        //etc...
        //etc...
    
        return true;
    }