First, you should read this
AndroidManifest.xml for your activity with searchview widget (singleTop is necessary, please read the link above):
<activity android:name=".activities.MainActivity"
android:configChanges="orientation|screenSize"
android:launchMode="singleTop" >
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
Create .../res/xml/searchable.xml with this:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/hint"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"/>
Modify onCreateOptionsMenu (last two lines are matter)
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_action_bar_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint(getResources().getString(R.string.hint));
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
}
And finally override onNewIntent():
@Override
protected void onNewIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doSearch(query);
}
}
After these changes you will see a mic button in a searchview.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…