Sagar Maiyad
Sagar Maiyad

Reputation: 12753

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.?

Upvotes: 2

Views: 1010

Answers (3)

Try this inside the Class and extend ActionBarActivity in your Class (Example: - public class Home extends ActionBarActivity)

@Override
public void onBackPressed() {
    super.onBackPressed()
}

For collapsing the SearchView: Try this,

menu.collapseActionView();

(or)

searchView = menuItem.getActionView().

(or)

searchView.onActionViewCollapsed()

Upvotes: 0

Ugur
Ugur

Reputation: 1729

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;
}

Upvotes: 2

3mpty
3mpty

Reputation: 1385

In onBackPressed call invalidateOptionsMenu() or store SearchView as Activity field and call searchView.onActionViewCollapsed(); but that can require additional work when restoring your Toolbar state (title state etc.).

Upvotes: 0

Related Questions