androidandroid-support-librarysearchviewandroid-actionmode

Search View with collapseActionView|always does not work on Action Menu


In an AppCompat activity with context action bar

Trying to add android.support.v7.widget.SearchView to the action mode menu the searchView doesn't expand

using appcompat-v7:25.2.0

menu Resource file

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_search"
        android:icon="@drawable/ic_search_dark_24dp"
        android:title="@string/search_list"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always|collapseActionView" />
    <item
        android:id="@+id/item_two"
        android:icon="@drawable/item_two"
        android:title="@string/item_two"
        app:showAsAction="ifRoom"
        >
    </item>
</menu>

Activity Theme

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionModeOverlay">false</item>
    <item name="actionModeBackground">@color/colorPrimaryDark</item>
</style>

Activity code

private class SelectionActionModeCallBack implements ActionMode.Callback {
        private SearchView mSearchView;

        SelectionActionModeCallBack() {


        }


        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            mode.getMenuInflater().inflate(R.menu.book_selection_action_menu, menu);//Inflate the menu over action mode


            mSearchView =
                    (SearchView) menu.findItem(R.id.action_search).getActionView();


            mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String s) {
                    startSearch(s);
                    return true;
                }

                @Override
                public boolean onQueryTextChange(String s) {
                    return false;
                }
            });
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            mSearchView.requestFocus();
            return true;
        }

This contradicts the documentation


Solution

  • Contrary to the case when using Search View in action bar the desired collapsible effect for search view while having other items in the action mode use

    app:showAsAction="always"
    

    without collapseActionView !