I need to show SearchView always expanded and one more button in ActionBar to give user quick access to search string in Search Activity.
I have this menu layout for my activity:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_search"
android:title="@string/action_search"
android:icon="@drawable/ic_action_search"
android:showAsAction="always"
android:actionViewClass="com.actionbarsherlock.widget.SearchView"/>
<item android:id="@+id/action_configure"
android:title="@string/action_configure"
android:icon="@android:drawable/ic_menu_preferences"
android:showAsAction="always" />
</menu>
And in Search activity class I configure ActionBar as follows:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.search, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView)menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setSubmitButtonEnabled(false);
return true;
}
The problem is that if I do searchView.setIconifiedByDefault(false);
I see this picture:
You can notice that there's no second menu item. Here, if I set android:showAsAction="always|collapseActionView", it becomes collapsed by default, but I can see second item now:
On the picture above I already pressed search icon
There are two problems that I can't figure out:
Here is my solution for hiding the inner search icon.
In your styles.xml where you manage your theme, add the following to your application_theme section (usually below the base_theme)
<!--Application Theme-->
<style name="application_theme" parent="application_base_theme">
<item name="searchViewSearchIcon">@android:color/transparent</item>
<item name="searchViewCloseIcon">@android:color/transparent</item>
</style>
This forces all occurrences of the search icon within the actionbar search widget to be transparent, and invisible.
They will still take up space in the text field though, creating an invisible margin at the left side where the icon used to be. If you want to fix this, you must instead extend com.actionbarsherlock.widget.SearchView with your own class and add the following:
ImageView searchHintIcon = (ImageView) findViewById(R.id.abs__search_mag_icon);
searchHintIcon.setVisibility(View.GONE);
I would add this to the Constructor but also override setIconified and setIconifiedByDefault and include it there as well. This will force it to hide the icon whenever you expand the action view.
For showing other icons in the action bar, try setting:
android:showAsAction="collapseActionView|ifRoom"
This should cause the search bar to expand when there is enough room in the action bar, instead of forcing them off of the bar entirely.
HTH
I managed to fix that by using
mySearchView.setIconfied(false);
rather than setIconifiedByDefault()
P.S. I was using android.support.v7.widget.SearchView
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With