Adding SearchView in Fragment

Answer updated for 2018. Sorry for Kotlin, if you're using Java.

Your xml menu resource (search_menu.xml for example):

<?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/action_search"
        android:title="@string/app_name"
        android:icon="@drawable/ic_search"
        app:showAsAction="collapseActionView|ifRoom"
        />
</menu>

Build-up code:

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater?) {
    super.onCreateOptionsMenu(menu, inflater)
    menu.clear()
    inflater?.inflate(R.menu.search_menu, menu)
    val searchView = SearchView((context as MainActivity).supportActionBar?.themedContext ?: context)
    menu.findItem(R.id.action_search).apply {
        setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW or MenuItem.SHOW_AS_ACTION_IF_ROOM)
        actionView = searchView
    }

    searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
        override fun onQueryTextSubmit(query: String): Boolean {
            return false
        }

        override fun onQueryTextChange(newText: String): Boolean {
            return false
        }
    })
    searchView.setOnClickListener {view ->  }
}

You'll probably also need to put this line in onCreate() (worked for my fragment) to get this all to appear:

setHasOptionsMenu(true)

Change your MenuSearch.xml to

    <?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/action_search"
            android:title="@string/app_name"
            android:icon="@drawable/ic_action_search"
            app:showAsAction="collapseActionView|ifRoom"
             />
    </menu>

And in your fragment, add searchView.setOnQueryTextListener

    @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            super.onCreateOptionsMenu(menu, inflater);
            menu.clear();
            inflater.inflate(R.menu.search_option_menu, menu);
            MenuItem item = menu.findItem(R.id.action_search);
            SearchView searchView = new SearchView(((MainActivity) mContext).getSupportActionBar().getThemedContext());
           // MenuItemCompat.setShowAsAction(item, //MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | //MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
           //  MenuItemCompat.setActionView(item, searchView);
           // These lines are deprecated in API 26 use instead
 item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItem.SHOW_AS_ACTION_IF_ROOM);
        item.setActionView(searchView);
            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    return false;
                }
                @Override
                public boolean onQueryTextChange(String newText) {
                    return false;
                }
            });
            searchView.setOnClickListener(new View.OnClickListener() {
                                              @Override
                                              public void onClick(View v) {

                                              }
                                          }
            );
        }