There is a search button in the menu. It is necessary either:

  • programmatically emulate pressing the search button in a menu or
  • activate SearchView widget

    Any way will do.

menu.xml:

<?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" xmlns:tools="http://schemas.android.com/tools" tools:context=".activity.MyActivity"> <item android:id="@+id/action_search" android:icon="@drawable/ic_search_white_24dp" android:title="@string/search_hint" app:showAsAction="always|collapseActionView" app:actionViewClass="android.support.v7.widget.SearchView" /> </menu> 

Activation code:

  boolean onCreateOptionsMenu( Menu menu) { getMenuInflater().inflate( R.menu.menu_main, menu); menuItem = menu.findItem( R.id.action_search); searchView = (SearchView) menuItem.getActionView(); return true; } 

There is a MenuItem menuItem and SearchView searchView, now you need to activate SearchView in some way.

    2 answers 2

    1. MenuItem menuItem; field MenuItem menuItem;

    2. Initialize it in onCreateOptionsMenu - this.menuItem = menu.findItem( R.id.action_search);

    3. Now activate (emulate click) like this: ((SearchView) menuItem.getActionView()).onActionViewExpanded();

    • menuItem.getActionView ()). onActionViewExpanded () partially triggered. The keyboard appears, and the search widget does not open. - Devel0per
    • @ Devel0per, try removing collapseActionView from app:showAsAction="always|collapseActionView" - Yuriy SPb
    • If you remove the collapseActionView, it works, thanks! - Devel0per
    • @ Devel0per, if the answer solves the problem stated in the question, then you can vote for it and mark it right by clicking on the up arrow and tick to the left of the response body) - Yuriyi б

    I found a solution that works with collapseActionView in the app:showAsAction="always|collapseActionView" .

    need to call menuItem.expandActionView()