Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show ActionBarSherlock SearchView always expanded

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:
enter image description here

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:
enter image description here
On the picture above I already pressed search icon

There are two problems that I can't figure out:

  1. How do I show always two elements: expanded search view and icon for configuration action?
  2. How do I get rid of this ugly second search icon?
like image 826
gorodechnyj Avatar asked Nov 28 '12 11:11

gorodechnyj


2 Answers

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

like image 198
sturrockad Avatar answered Nov 14 '22 02:11

sturrockad


I managed to fix that by using

mySearchView.setIconfied(false); 

rather than setIconifiedByDefault()

P.S. I was using android.support.v7.widget.SearchView

like image 32
moon4e Avatar answered Nov 14 '22 02:11

moon4e