0

A common pattern I see in many apps that provide some search functionality is to have the search widget clear the other menu icons off of the ActionBar. This works for me right now just by using ifRoom for showAsAction in my menu items. I'd like to show more menu items in the actionbar though, because it seems that the ActionBar is very conservative with how it determines ifRoom and consequently at least one important menu item is pushed to the overflow (perhaps someone smarter than me knows why they do that).

So how can I set showAsAction as always while still clearing the menu items off of the ActionBar when the Search Widget is activated? When I try that now, the menu items don't move, and the search widget is condensed.

Here's my menu:

  <item
  android:id="@+id/action_search"
  android:icon="@drawable/ic_action_search"
  android:title="@string/action_search"
  android:showAsAction="collapseActionView|always"
  android:actionViewClass="android.widget.SearchView" />

 <item
  android:id="@+id/action_stores"
  android:title="@string/action_stores"
  android:icon="@drawable/ic_action_stores"
  android:showAsAction="always" />

 <item 
   android:id="@+id/action_shopping_lists"
   android:title="@string/action_shopping_lists"
   android:icon="@drawable/ic_action_lists"
   android:showAsAction="always" />

<item
    android:id="@+id/action_preferences"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="never"/>

My searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android" 
            android:label="@string/app_name"
            android:hint="@string/search_hint" >
</searchable>
settheline
  • 3,333
  • 8
  • 33
  • 65

1 Answers1

0

Couldn't you set an onClickListener on the search widget that then runs some few lines of code to make the other MenuItems change from 'always' to 'ifRoom'?

The few lines of code within the onClickListener would contain something like this:

myMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

whereas myMenuItem is your MenuItem that should go from 'always' to 'ifRoom'. You can change it back later in an appropriate method.

REG1
  • 486
  • 4
  • 15