I installed SearchWidget as described in the Android API Guide . It correctly displays the magnifying glass icon in the action bar, and if I click on it, it will launch the search widget in the action bar (input field with a dark background).
However, if I click the search button for the virtual device, another search field will open: it has a white background and does not call the callback methods that 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; }
Customizing the search widget is exactly the same as 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 I tried the setIconfied () method is because the Android API Guide says the "Ability to switch the visibility of the search box" section:
βBy default, the search widget isβ marked β, which means that it is represented only by the search sign (magnifying glass) and expands to show the search box when the user touches it. As shown above, you can display the default search box by calling setIconifiedByDefault (false ). You can also switch the lookup widget by calling setIconified (). "
I also tried calling startSearch("", false, null, false); but this leads to the same search field with a white background.
So my question is: is it possible to launch the search widget in the action bar if the user clicks the deviceβs search button, i.e. forms exactly the same action as if you clicked on a search menu item?
android android-widget android-searchmanager
Michael osl
source share