I need to intercept when my SearchView is collapsing or not, in order to change some fragments in my Activity, that is an AppCompatActivity. I created a listener with the method MenuItemCompat.setOnActionExpandListener but it seems that it's never called. Also strange thing, in my method onOptionsItemSelected, after some debug, I checked that still here I can't itercept nothing. I tried various method founded here about this problem, but no way to resolve it. Here is my code:
menu_search_cat.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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".CategoriesActivity">
<item android:id="@+id/search"
android:title="@string/search_title"
android:orderInCategory="1"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="always"
app:actionViewClass="android.widget.SearchView" />
CategoriesActivity
import android.widget.SearchView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_categories);
...
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_search_cat, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
final SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(this);
MenuItem searchItem = menu.findItem(R.id.search);
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "onSearchViewExpand");
}
toggleDown();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_from_right, R.anim.slide_out_to_left);
SuggestionsFragment newFragment = SuggestionsFragment.newInstance(mBusiness);
ft.replace(R.id.category_container, newFragment, "suggestionsFragment");
ft.addToBackStack(null);
ft.commit();
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "onSearchViewCollapse");
}
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_from_left, R.anim.slide_out_to_right);
MacroCategoriesFragment newFragment = new MacroCategoriesFragment();
ft.replace(R.id.category_container, newFragment, "macrocatFragment");
ft.commit();
return true;
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "onOptionsItemSelected: "+item.toString());
}
// handle arrow click here
if (item.getItemId() == android.R.id.home) {
onBackPressed();
}
if (item.getItemId() == R.id.search) {
//never work here
}
return super.onOptionsItemSelected(item);
}
I solved the problem: the onActionExpandListener works only if the ActionView of my MenuItem is marked also as collapseActionView in my menu file. So I modified it in this way:
<item
android:id="@+id/search"
android:title="@string/search_title"
android:orderInCategory="1"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.widget.SearchView" />