Show SearchWidget in ActionBar if user presses the device's search button

放肆的年华 提交于 2019-12-30 06:20:08

问题


I set up a SearchWidget as described in the Android API Guide. It properly displays a magnifying glass icon in the Action Bar and if I click on it, it launches the search widget within the Action Bar (an input field with dark background).

However, if I press the virtual device's search button, then a different search field is launched: It has a white background and does not invoke the callback methods I specified.

So I added this to my MainActivity class:

public boolean onSearchRequested() {
    SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
    searchView.setIconified(!searchView.isIconified());
    return false;
}

The set up of the search widget is exactly the same as it is described in the docs:

SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(new CustomSearchQueryTextListener());

My searchable.xml:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name">
</searchable>

And AndroidManifest.xml:

    <activity
        android:name="MainActivity"
        android:label="@string/app_name"
        android:launchMode="singleTask"
        android:uiOptions="splitActionBarWhenNarrow" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/>
    </activity>

The reason why I tried the setIconfied() method is that in the Android API Guide it says under "The ability to toggle the search box visibility":

"By default, the search widget is "iconified," meaning that it is represented only by a search icon (a magnifying glass), and expands to show the search box when the user touches it. As shown above, you can show the search box by default, by calling setIconifiedByDefault(false). You can also toggle the search widget appearance by calling setIconified()."

I also tried to call startSearch("", false, null, false); but this brings up the same search field with the white background.

So my question is: Is it possible to launch the search widget within the action bar if the user presses the device's search button, i.e. peform exactly the same action as if the search menu item is clicked?


回答1:


While writing this question I came up with the solution by myself. I still decided to post it along with the answer, because it does not seem like this question has been asked on SO before and maybe someone encounters the same problem and/or comes up with a better solution.

All you need to do is call expandActionView() on the search menu item, and everything works like expected:

public boolean onSearchRequested() {
    menu.findItem(R.id.menu_search).expandActionView();
    return true;
} 


来源:https://stackoverflow.com/questions/14003036/show-searchwidget-in-actionbar-if-user-presses-the-devices-search-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!