0

I know this question occurred here a few times, but none of the results helped me.

I want to keep my SearchView always expanded. When I enter the activity it's expanded, when I press the back button for the first time the keyboard goes down (that's ok) and when I press the back button again - the SearchView closes instead of going back to the previous activity.

This is my code in the activity:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_search_series, menu);

    // Associate searchable configuration with the SearchView
    SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);

    // Get the SearchView item
    MenuItem item = menu.findItem(R.id.search);
    SearchView searchView = (SearchView) item.getActionView();

    searchView.setSearchableInfo(
            searchManager.getSearchableInfo(new ComponentName(this, SearchSeriesActivity.class)));

    // Make it open from the start
    item.expandActionView();

    searchView.setOnQueryTextListener(this);

    return true;
}

This is the xml:

<item android:id="@+id/search"
    android:title="@string/action_search_series"
    android:icon="@mipmap/ic_search_white"
    app:showAsAction="always|collapseActionView"
    app:actionViewClass="android.support.v7.widget.SearchView" />

I tried all the solutions:

  • Replacing the app:showAsAction to only "always" of only "ifRoom" didn't open it at all.
  • Setting the android:iconifiedByDefault to false or true - non of them worked, not in the code also.

Thank you!

Rotem Shaanan
  • 83
  • 1
  • 10

1 Answers1

0

I had the same problem as you have and I figure it out that the best solution, in my case, was to create a custom Search Bar, without taking in consideration Google's one, because, not only I had the problem you're having right now, but also others related to its styling.

So, it might be useful look at my solution here: Styling Search View on Android (min21)

Community
  • 1
  • 1
Cesarsk
  • 153
  • 1
  • 2
  • 14