Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Other options menu items not showing after expanded searchview is collapsed

I have a simple activity with options menu setup with the code shown below. The problem i'm experiencing is that after expanding and collapsing the searchview, menu items with app:showAsAction="ifRoom" do not appear, even though there is room on the toolbar. Menu items with app:showAsAction="never" on the other hand do appear.

menu_simple_activity.xml

<?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_white_24dp"
        android:title="@string/action_search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always|collapseActionView" />
    <item
        android:id="@+id/action_filter"
        android:icon="@drawable/ic_filter_list_white_24dp"
        android:title="@string/action_filter"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/action_sort"
        android:icon="@drawable/ic_sort_white_24dp"
        android:title="@string/action_sort"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/action_print_all"
        android:icon="@drawable/ic_print_white_24dp"
        android:title="@string/action_print_all"
        app:showAsAction="never" />
</menu>

SimpleActivity.java

public class SimpleActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", 
                Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        getMenuInflater().inflate(R.menu.menu_simple_activity, menu);
        MenuItem searchMenuItem = menu.findItem(R.id.action_search);
        final MenuItem filterMenuItem = menu.findItem(R.id.action_filter);
        final MenuItem sortMenuItem = menu.findItem(R.id.action_sort);
        final MenuItem printAllMenuItem = 
        menu.findItem(R.id.action_print_all);

        searchMenuItem.setOnActionExpandListener(new 
        MenuItem.OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                filterMenuItem.setVisible(false);
                sortMenuItem.setVisible(false);
                printAllMenuItem.setVisible(false);
                return true;
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                filterMenuItem.setVisible(true);
                sortMenuItem.setVisible(true);
                printAllMenuItem.setVisible(true);
                return true;
            }
        });
        return true;
    }
}

How do i fix it?

like image 510
Edwin Nyawoli Avatar asked Nov 28 '17 15:11

Edwin Nyawoli


1 Answers

You should call invalidateOptionsMenu() after setting the items as visible.

From the documentation:

Declare that the options menu has changed, so should be recreated. The onCreateOptionsMenu(Menu) method will be called the next time it needs to be displayed.

like image 85
LordRaydenMK Avatar answered Sep 28 '22 02:09

LordRaydenMK