I'm developing an app for Android 3.2 and greater with android-support-v4
. I need to implement OnActionExpandListener
for "intercept" when SearchView in the actionbar is expanded and when is collapsed. My code for Android 4.0 and higher it's ok, but for 3.2 no.
menu.xml
<item android:id="@+id/menu_search"
android:title="@string/menu_search"
android:icon="@android:drawable/ic_menu_search"
android:showAsAction="collapseActionView|always"
android:actionViewClass="android.widget.SearchView" />
MyActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.reader, menu);
final MenuItem searchMI = menu.findItem(R.id.menu_search);
if(searchView == null) {
//searchView = (SearchView) searchMI.getActionView();
searchView = (SearchView) MenuItemCompat.getActionView(searchMI);
searchView.setOnQueryTextListener(this);
searchView.setOnSuggestionListener(this);
searchView.setOnCloseListener(new OnCloseListener() {
@Override
public boolean onClose() {
//some code
return false;
}
});
}
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR2) {
MenuItemCompat.setShowAsAction(searchMI, MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
MenuItemCompat.setOnActionExpandListener(searchMI, new OnActionExpandListener() {
/* (non-Javadoc)
* @see android.support.v4.view.MenuItemCompat.OnActionExpandListener#onMenuItemActionExpand(android.view.MenuItem)
*/
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
Toast.makeText(getApplicationContext(), "onMenuItemActionExpand", Toast.LENGTH_SHORT).show();
return true;
}
/* (non-Javadoc)
* @see android.support.v4.view.MenuItemCompat.OnActionExpandListener#onMenuItemActionCollapse(android.view.MenuItem)
*/
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
Toast.makeText(getApplicationContext(), "onMenuItemActionExpand", Toast.LENGTH_SHORT).show();
return true;
}
});
} else {
searchMI.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
Toast.makeText(getApplicationContext(), "MenuItem#onMenuItemActionExpand", Toast.LENGTH_SHORT).show();
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
Toast.makeText(getApplicationContext(), "MenuItem#onMenuItemActionExpand", Toast.LENGTH_SHORT).show();
return true;
}
});
}
}
Why, for Honeycomb, methods of listener is not invoked?
Thank you so much.
You probably missed the fact (like I did) that `MenuItemCompat.OnActionExpandListener' interface has a static implementation, and is not an instance method.
So, if you have a class that implements
MenuItemCompat.OnActionExpandListener
then in that class you need to install it as the listener like this:The same paradigm applies to setActionView ... rather than invoke
menuItem.setActionView(this)
, you pass the menuItem as the first argument to the static versionMenuItemCompat.setActionView
and follow with the other argument(s).For MenuItemCompat.setOnActionExpandListener to work you should add "collapseActionView" added in the menu item - for example -
And in the onCreateOptionsMenu you can use it this way -
thanks for your help, your solution is work for me. and i'd like to vote you up, but i just realized i have only 1 reputation,(;′⌒`)
actually, my solution is similar to your, there is just one different in the menu xml file like this:
I found that
MenuItemCompat.setOnActionExpandListener(...)
is not working if you don't pass:But this is changing the SearchView and is replacing the DrawerToggle with back arrow.
I wanted to keep the original views and still track the Expanded/Collapsed state and use supported Search View.
Solution:
When
android.support.v7.widget.SearchView
is changing the view state the LinearLayout view's, with idandroid.support.v7.appcompat.R.id.search_edit_frame
, visibility value is being changed fromView.VISIBLE
toView.GONE
and opposite. So I add ViewTreeObserver to track the visibility change of the search edit frame.menu_search.xml:
In the activity:
Thanks!
Your Listener should be MenuItemCompat.OnActionExpandListener() .