Reputation: 151
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 ?
Upvotes: 7
Views: 1740
Reputation: 1025
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
Upvotes: 4
Reputation: 369
To overcome the ugly search bar resulting from showAsAction="always"
, I solved this way (keeping showAsAction="collapseActionView|ifRoom"
):
public boolean onCreateOptionsMenu(Menu menu) {
...
if (!TextUtils.isEmpty(mQuery))
new Handler().post(() -> {
searchView.clearFocus();
searchView.setIconified(false);
searchItem.expandActionView();//or MenuItemCompat.expandActionView for older devices
searchView.setQuery(mQuery, false);//or true here if you want it submitted!
});
}
The trick is to call searchItem.expandActionView()
after searchView.setIconfied(false)
.
For increased stability, I keep the whole sequence in a post
- as you did - but it looks like is working better to have a new Handler()
to do it.
Upvotes: 1
Reputation: 11
I had similar problem and solved it like this.
In onCreateOptionsMenu:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu, menu);
MenuItem item = menu.findItem(R.id.search_option);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(searchQueryListener);
if(searchQuery!=null && !searchQuery.equals("")) {
searchView.setIconified(false);
searchView.setQuery(searchQuery,true);
searchView.clearFocus();
}
}
For the searchView menu item:
<item
android:id="@+id/search_option"
android:title="@string/button_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"/>
In my case I have another menu item with icon to the right of the search view and this way it works fine when rotating the device.
Upvotes: 1