2

First, please help me rephrase the title to suits the issue better, for others to find.
This question has been asked here but has been answered not to my satisfaction, and I will explain why.

Note: I use the v7-AppCompat Support Library ~v22 with a Toolbar and setSupportActionBar(toolbar)

Question
How to constrain an ActionView (SearchView in my case) of an expanded MenuItem to fill parent but not push other MenuItems outside of scope.

Obvious (not good enough) Answers

  1. In code, we can use MenuItemCompat.expandActionView(item); on the item, but:
    a. When expanded, the "back" button is shown on top of the DrawerToggle. This doesn't really solve it.
    b. On back presses the SearchView will collapse again.

  2. As known, setting iconifiedByDefault(false) on the SearchView, along with app:showAsAction="ifRoom|collapseActionView" makes it expanded (and with some side effects), but: see '1' above.

My Current Solution
I use a custom width factor (~0.7) on the 2nd solution, forcing the SearchView to be as width as (Toolbar's width) * (factor).
This is somewhat fine, but sometimes fails (I guess menu is not fully prepared), and it IS possible, having that normally expanding the SearchView (1st solution) leads to the desired width.

Another thing I have tried:
Setting the SearchView as the Toolbar's custom layout, instead of a MenuItem. But than it starts collapsed and I can't seem to expand it. Also, I would like to do this the "right" way and not use custom layouts.

I will gladly put some more code in if that helps in any way.
Thx!

Community
  • 1
  • 1
guy_m
  • 3,110
  • 2
  • 19
  • 33

1 Answers1

2

It seems that, the action bar restricts the max with of your SearchView. The following code works for me :

 final MenuItem item = menu.findItem(R.id.search);
 item.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItem.SHOW_AS_ACTION_ALWAYS);
 mSearchView = (SearchView) MenuItemCompat.getActionView(item);
 mSearchView.setMaxWidth(Integer.MAX_VALUE);

Core trick is to change the maxWith of a SearchView.

Hope this helps you .

zccneil
  • 139
  • 11
  • Yes, that is somehow "My Current Solution" in the question above... I'm looking for something dynamic.. thx though! – guy_m Aug 29 '16 at 11:07
  • By "dynamic", do you mean the width will change according to the number of menu items? This method, only change the maxWith rather than calculate the with according to the items. Thus, when you change the item numbers, your searchView will change. – zccneil Aug 30 '16 at 06:21