简体   繁体   中英

Toolbar icons disappearing after expanding the SearchView in Android

here's my problem:

I have that nice toolbar with the icons in landscape mode: 在此处输入图片说明

after expanding the search view and showing the popup menu the "add" item appears (I thought that it shouldn't):

在此处输入图片说明

then returning with the back arrow key, as you see, the add button goes:

在此处输入图片说明

and you won't find it in the popup menu anymore: 在此处输入图片说明

I'm using support:appcompat-v7:25.1.0, and here's my menu code:

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/app_bar_search"
        android:actionViewClass="android.support.v7.widget.SearchView"
        android:icon="@drawable/ic_action_search"
        android:title="Search"
        app:showAsAction="always|collapseActionView"
        android:enabled="true"
        android:visible="true"
        app:actionViewClass="android.support.v7.widget.SearchView"/>
    <item android:title="Add"
        android:enabled="true"
        android:icon="@drawable/ic_action_add"
        android:visible="true"
        app:showAsAction="ifRoom"
        android:id="@+id/add" />
    <item android:title="Settings"
        android:id="@+id/settings"
        app:showAsAction="never"
        android:icon="@drawable/ic_action_settings"
        android:enabled="true"
        android:visible="true" />
    <item android:title="Feedback"
        android:id="@+id/feedbvack"
        app:showAsAction="never"
        android:icon="@drawable/ic_action_feedback"
        android:enabled="true"
        android:visible="true" />

</menu>

I can set the add button showAsAction to "always" but I know that this is discouraged. Does anyone here know why is there this behavior? and how can I prevent that?

Thanks in advance.

Try this

<item
    android:id="@+id/app_bar_search"
    android:actionViewClass="android.support.v7.widget.SearchView"
    android:icon="@drawable/ic_action_search"
    android:title="Search"
    app:showAsAction="always|collapseActionView"
    android:enabled="true"
    android:visible="true"
    app:actionViewClass="android.support.v7.widget.SearchView"/>
<item android:title="Add"
    android:enabled="true"
    android:icon="@drawable/ic_action_add"
    android:visible="true"
    app:showAsAction="always"
    android:id="@+id/add" />
<item android:title="Settings"
    android:id="@+id/settings"
    app:showAsAction="never"
    android:icon="@drawable/ic_action_settings"
    android:enabled="true"
    android:visible="true" />
<item android:title="Feedback"
    android:id="@+id/feedbvack"
    app:showAsAction="never"
    android:icon="@drawable/ic_action_feedback"
    android:enabled="true"
    android:visible="true" />

Set app:showAsAction to always to make sure it will be visible always.

Use of ifRoom has this feature if space available it will show or else it will hide it better use always or never

<item android:title="Add"
    android:enabled="true"
    android:icon="@drawable/ic_action_add"
    android:visible="true"
    app:showAsAction="always"
    android:id="@+id/add" />

You can try to use this:

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

    @Override
    public boolean onMenuItemActionCollapse(MenuItem item) {
        supportInvalidateOptionsMenu();
        //or use invalidateOptionsMenu();
        return true;
    }
});

So when SearchView will be collapsed Toolbar will reallocate items and ifRoom items will be visible. I had this bug too and solved it this way.

@Kovy has answered above with the fix for this bug and I confirm it fixes the bug. Thank you very much, mate! However, the above function has been deprecated in favour of individual menu items OnActionExpandListener. Example of it is as posted below:

MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.menu, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            supportInvalidateOptionsMenu();
            return true;
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM