Search code examples
androidandroid-actionbarsearchview

expandActionView in onCreateOptionsMenu removes or hides other menu items from action bar permanently


I have a searchView in action bar and when user has query in searchView and then rotates the device i set query in searchView in onCreateOptionsMenu and expand searchView with menuItem.expandActionView() because searchView.setIconified(false) has no effect for me but calling menuItem.expandActionView() removes or hides other menu items even after user closes searchView other menu items remains hidden!! after searcheView closed by user even searchView icon is not showing in action bar other menu item and serachView has showAsAction set to ifRoom

i am surprised as when user expands action view by clicking the search icon there is no problem but when i call expandActionView i have this problem !

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    final MenuItem searchMenuItem = menu.findItem(R.id.searchMenuItem);
    searchView = (SearchView) searchMenuItem.getActionView();
    searchView.setOnQueryTextListener(this);
    searchView.setOnCloseListener(this);
    searchView.setSubmitButtonEnabled(false);

    searchView.setIconifiedByDefault(true);// <-- this line has no effect even when using false !!

    if(initialQuery != null) {

        searchView.setIconified(false);// <-- this line has no effect !!!

        searchView.setQuery(initialQuery, true);
        searchMenuItem.expandActionView();//<-- this line expands searchView but hides other menu items!!
        searchView.setQuery(initialQuery, true);
    }
    return super.onCreateOptionsMenu(menu);
}

Solution

  • The solution that i have used and is working for me is to use always or never instead of ifRoom for showAsAction