1

When adding a SearchView to the menu (see example below), it's icon doesn't change and it behaves in a non-standard way. For example, long clicking on the search menu button should have popped the text Search, but it doesn't.

How do I change its behavior to be more standard? (icon and long click for now. I'm sure there are other issues)

Code sample for adding the view:

MenuItem item = menu.add("Search");
SearchView sv = new SearchView(getActivity());
item.setActionView(sv);
item.setIcon(drawable.menu_search);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

Thanks.

AlikElzin-kilaka
  • 34,335
  • 35
  • 194
  • 277

1 Answers1

7

Update: It is possible from code. You have to add MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW and change the context of the SearchView to getActionBar().getThemedContext().

MenuItem item = menu.add("Search");
SearchView sv = new SearchView(getActionBar().getThemedContext());
item.setActionView(sv);
item.setIcon(R.drawable.ic_search);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
        | MenuItem.SHOW_AS_ACTION_IF_ROOM);

You are trying to create the SearchView action item from code which is not the suggested way to do it. Define a menu resource containing the search item like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/search"
        android:actionViewClass="android.widget.SearchView"
        android:icon="@drawable/ic_search"
        android:showAsAction="collapseActionView|ifRoom"
        android:title="@string/search_title"/>

</menu>

Inflate it in onCreateOptionsMenu:

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

This will solve the problems. For more information read Add the Search View to the Action Bar from the Android documentation.

Matthias Robbers
  • 15,689
  • 6
  • 63
  • 73
  • Because you don't find it in the documentation. I did not find it anywhere in the internet either. You need access to the `android:actionViewClass` attribute which you can't get from code. Why not do it the way Android suggests? I don't see disadvantages in having menu resources. – Matthias Robbers May 05 '13 at 12:14
  • Have to correct myself here. Your approach is possible, too. See my updated answer. – Matthias Robbers May 05 '13 at 16:57
  • The code you added did the trick. Thanks. I still have some issues though: 1. Shown at the top instead of the split actionbar. 2. Keeps the content focusable, even though moved to search context. – AlikElzin-kilaka May 06 '13 at 10:28
  • I can't answer that without more information. Edit your question or better, open a new question. And I have no idea what you mean with "even though moved to search context". – Matthias Robbers May 06 '13 at 17:29
  • What I mean is that the behavior is still deferent than the standard search in one (important IMO) thing - the search text is shown with the still viewed and inter-actable content. This, I guess, can be handled manually but still comment worthy. – AlikElzin-kilaka May 07 '13 at 10:17