gorodechnyj
gorodechnyj

Reputation: 691

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?

Upvotes: 7

Views: 7616

Answers (3)

moon4e
moon4e

Reputation: 316

I managed to fix that by using

mySearchView.setIconfied(false); 

rather than setIconifiedByDefault()

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

Upvotes: 3

sturrockad
sturrockad

Reputation: 4550

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

Upvotes: 3

My way to solve the showing of two elements was setting a specific width to SearchView.

FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(350, FrameLayout.LayoutParams.WRAP_CONTENT);
searchView.setLayoutParams(lp);

The code that i am using is:

res/menu/main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/search"
        android:actionViewClass="com.actionbarsherlock.widget.SearchView"
        android:showAsAction="always"
        android:title="@string/action_search"/>
    <item
        android:id="@+id/settings"
        android:showAsAction="ifRoom"
        android:onClick="startSettingsActivity"
        android:title="@string/action_settings"/>
</menu>

And in my Activity

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.main, menu);
    MenuItem searchViewItem = menu.findItem(R.id.search);
    SearchView searchView = (SearchView) searchViewItem.getActionView();
    FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(350, FrameLayout.LayoutParams.WRAP_CONTENT);
    searchView.setLayoutParams(lp);

    searchView.setIconifiedByDefault(false);
    searchView.setSubmitButtonEnabled(false);

    return true;
}

public void startQuickSearchActivity(MenuItem menuItem){
    startActivity(new Intent(this, QuickSearchActivity_.class));
}

Upvotes: 0

Related Questions