I'm having a bad time with my Searchview on Android dev.
I'm trying to implement the Searchview on my toolbar's app. For now it's working very well but when I rotate the device, the search view behaves strangely.
My objective is persist the query text, so on "OnCreateOptionsMenu"
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_funcionarios_toolbar, menu);
clearSearchItem = menu.findItem(R.id.botao_limpar_busca);
searchItem = menu.findItem(R.id.botao_buscar);
searchItem.setIcon(new IconicsDrawable(getContext(), MaterialDesignIconic.Icon.gmi_search).sizeDp(24));
searchView = (SearchView) searchItem.getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setOnQueryTextListener(searchViewTextListener);
searchView.post(new Runnable() {
@Override
public void run() {
if (!mQuery.equals("")) {
searchView.setQuery(mQuery,false);
}
}
});
if (mQuery != null & !mQuery.equals("")) {
searchItem.expandActionView();
}
}
But If I use .expandActionView(), after the rotation (and cleaning the field using the "X" button provided by the SearchView), the MenuItem turns into those 3 little dots without any action. To restore the previous behaviour, I have to rotate the device again.
I'm almost sure the problem is on my XML:
<item
android:id="@+id/botao_buscar"
android:title="@string/menu_buscar"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"/>
Changing the "showAsAction" attribute to "always" worked like a charm.
Any thoughts on this ?