5

I'm facing some problems with a SearchView in a Contextual Action Bar called by a Fragment. The major one is that when my SearchView is expanded, it makes disappear all other items in action bar (except the closing button) even if there's some unused space, like you can see in this screenshot:

Screenshot

Furthermore, I'm also having the same exact problem discussed in this question: ActionBar always expanded SearchView with the icon inside

This is the XML code of my SearchView:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/menu_series_cab_search"
        android:icon="@drawable/ic_search"
        android:title="@string/action_search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="ifRoom"/>
    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100"
        app:showAsAction="never" />
</menu>

And this is the JAVA code of my Fragment that implements ActionMode.Callback

@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    mode.getMenuInflater().inflate(R.menu.menu_series_cab, menu);
    mSearchView = (SearchView) MenuItemCompat
            .getActionView(menu.findItem(R.id.menu_series_cab_search));
    mSearchView.setQueryHint(getResources().getString(R.string.action_search));
    mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String s) {
            mSearchView.clearFocus();
            return true;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            return false;
        }
    });
    mSearchView.setIconifiedByDefault(false);
    return true;
}

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
    mSearchView.requestFocus();
    return true;
}
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
MatteoBelfiori
  • 280
  • 3
  • 13

2 Answers2

11

first thing the searchview widget has a maximum fixed size, so empty space are inevitabile.

If you want that your "action_settings" item doesn't disappear when the searchview collapse you need to set app:showAsAction="always"

<?xml version="1.0" encoding="utf-8"?>

<item android:id="@+id/menu_series_cab_search"
    android:icon="@drawable/ic_search"
    android:title="@string/action_search"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="ifRoom|collapseActionView"/>
<item android:id="@+id/action_settings"
    android:title="@string/action_settings"
    android:icon="@drawable/abc_ic_menu_moreoverflow_mtrl_alpha"
    app:showAsAction="always" />

And your Java code should be like this:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    inflater.inflate(R.menu.main, menu);


    MenuItem searchItem = menu.findItem(R.id.action_search);
    // Get the SearchView and set the searchable configuration
    searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
    searchView = (SearchView) MenuItemCompat.getActionView(searchItem);//MenuItemCompat.getActionView(searchItem);//menu.findItem(R.id.action_search).getActionView();
    // Assumes current activity is the searchable activity
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
    searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default

    super.onCreateOptionsMenu(menu, inflater);
}
Bronx
  • 4,480
  • 4
  • 34
  • 44
  • Hi Bronx, thank you for you answer, but it doesn't work. I've tryied with `app:showAsAction="always"` and also by removing `android:orderInCategory="100"`, but no luck. – MatteoBelfiori Mar 21 '15 at 13:56
  • 1
    Hi Galaron, i've updated my answer, the search item missed the "collapseActionView" action – Bronx Mar 21 '15 at 14:03
  • 1
    With your updated answer my other action is visible, but doesn't appear as an "overflow menu" (the three dots one above other) and, much more important, my searchview starts iconified and won't open even if I click on it. Sorry if I may appear someway noobish, but it was a very long time since I last coded for Android. – MatteoBelfiori Mar 21 '15 at 14:22
  • Thanks a lot Bronx, major problem is now solved. I've updated my Java code as you said and in XML I've just modified the first item, setting `app:showAsAction="ifRoom|collapseActionView"` (haven't touched the second). Now there's just a thing that I don't like, why the magnify icon appears outside the text field? For the rest I would +1 but I haven't enough reputation, thanks again. – MatteoBelfiori Mar 21 '15 at 15:12
  • no problem :), for the last thing i hope this can helps http://nlopez.io/how-to-style-the-actionbar-searchview-programmatically/ – Bronx Mar 21 '15 at 15:55
1

I'm completing this answer thanks to link posted by Bronx in his last post and other answer like this Android - cannot find TextView inside SearchWidget when using Android Support library (accepted answer): to move magnify icon inside text view, I remove this line from first Bronx answer

searchView.setIconifiedByDefault(false);

Then I've pasted this just abfter Bronx's code:

    AutoCompleteTextView autoComplete = (AutoCompleteTextView)mSearchView.findViewById(R.id.search_src_text);
    Class<?> clazz = null;
    try {
        clazz = Class.forName("android.widget.SearchView$SearchAutoComplete");
        SpannableStringBuilder stopHint = new SpannableStringBuilder("  ");
        stopHint.append(getString(R.string.action_search));
        // Add the icon as an spannable
        Drawable searchIcon = getResources().getDrawable(R.drawable.abc_ic_search_api_mtrl_alpha);
        Method textSizeMethod = clazz.getMethod("getTextSize");
        Float rawTextSize = (Float)textSizeMethod.invoke(autoComplete);
        int textSize = (int) (rawTextSize * 1.25);
        searchIcon.setBounds(0, 0, textSize, textSize);
        stopHint.setSpan(new ImageSpan(searchIcon), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        // Set the new hint text
        Method setHintMethod = clazz.getMethod("setHint", CharSequence.class);
        setHintMethod.invoke(autoComplete, stopHint);
    } catch (Exception e) {
        // Set default hint
        mSearchView.setQueryHint(getResources().getString(R.string.action_search));
        e.printStackTrace();
    }

Just pay attention that R.string.action_search refers to a custom string in my project, so change it with yours. Thanks again to Bronx.

Community
  • 1
  • 1
MatteoBelfiori
  • 280
  • 3
  • 13