Searchview after rotation - Button goes to the "3 dots"

Viewed 1523

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 ?

3 Answers

Finally, I found a solution in this site: LINK. (Thank you so much for this post.)

I copy the code here because It's could be a dependency.

<item
    android:id="@+id/menu_main_action_search"
    android:icon="@android:drawable/ic_menu_search"
    android:title="@string/menu_main_search_title"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="always|collapseActionView" />

Code:

searchMenuItem.expandActionView();
mSearchView.setQuery(mSearchString, true);
mSearchView.clearFocus();

Finally the 3 dot not appear again. There is a search icon !! awesome

Related