I have five action menu items in the action bar, which I\'m using action bar sherlock library as follows :
In onCreateOptionsMenu()
, i used the followin
This question is a bit old but I'll try to answer it anyway. I hope someone will find it useful.
Here is what I have done:
Create a custom SearchView class that extends SearchView. It defines 2 events: one fired when the search view expands and the other when it collapses.
public class EnglishVerbSearchView extends SearchView {
OnSearchViewCollapsedEventListener mSearchViewCollapsedEventListener;
OnSearchViewExpandedEventListener mOnSearchViewExpandedEventListener;
public EnglishVerbSearchView(Context context) {
super(context);
}
@Override
public void onActionViewCollapsed() {
if (mSearchViewCollapsedEventListener != null)
mSearchViewCollapsedEventListener.onSearchViewCollapsed();
super.onActionViewCollapsed();
}
@Override
public void onActionViewExpanded() {
if (mOnSearchViewExpandedEventListener != null)
mOnSearchViewExpandedEventListener.onSearchViewExpanded();
super.onActionViewExpanded();
}
public interface OnSearchViewCollapsedEventListener {
public void onSearchViewCollapsed();
}
public interface OnSearchViewExpandedEventListener {
public void onSearchViewExpanded();
}
public void setOnSearchViewCollapsedEventListener(OnSearchViewCollapsedEventListener eventListener) {
mSearchViewCollapsedEventListener = eventListener;
}
public void setOnSearchViewExpandedEventListener(OnSearchViewExpandedEventListener eventListener) {
mOnSearchViewExpandedEventListener = eventListener;
}
}
In my activity, I did the following: - Created a private field to store the reference to the menu. - Defined the events for collapsing and expanding the search view where I hide and show other actions by using the reference to the menu.
public class DictionaryActivity extends ActionBarActivity {
private Menu mMenu;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.dictionary, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
EnglishVerbSearchView searchView = (EnglishVerbSearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnSearchViewCollapsedEventListener(listenerCollapse);
searchView.setOnSearchViewExpandedEventListener(listenerExpand);
mMenu = menu;
return super.onCreateOptionsMenu(menu);
}
final private OnSearchViewCollapsedEventListener listenerCollapse = new OnSearchViewCollapsedEventListener() {
@Override
public void onSearchViewCollapsed() {
// show other actions
MenuItem favouriteItem = mMenu.findItem(R.id.action_favourite);
MenuItem playItem = mMenu.findItem(R.id.action_play);
favouriteItem.setVisible(true);
playItem.setVisible(true);
// I'm doing my actual search here
}
};
final private OnSearchViewExpandedEventListener listenerExpand = new OnSearchViewExpandedEventListener() {
@Override
public void onSearchViewExpanded() {
// hide other actions
MenuItem favouriteItem = mMenu.findItem(R.id.action_favourite);
MenuItem playItem = mMenu.findItem(R.id.action_play);
favouriteItem.setVisible(false);
playItem.setVisible(false);
}
};
}
In the menu.xml file, I used the custom search view:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="@+id/action_search"
android:title="@string/action_search"
android:icon="@drawable/action_search"
yourapp:showAsAction="always|collapseActionView"
yourapp:actionViewClass="com.xxx.EnglishVerbSearchView" />
</menu>
I hope it's all that's needed as it's just an extract from my full code of the activity. Let me know if there's anything missing.
As per comment from arne.jans:
It seems to be enough to do MenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
to make room for the SearchView
. The advantage would be: the other menu-items go into the overflow menu and are still accessible, even with the opened SearchView
.