Skip to content Skip to sidebar Skip to footer

Actionbar Mic Button(voice Search) In Searchview

I am implementing Action Bar. I want 2 buttons in Search field area of Action Bar SearchView like Flipkart app. Like above screenshot, I want 2 buttons, first one is for Voice Se

Solution 1:

First, you should read this

AndroidManifest.xml for your activity with searchview widget (singleTop is necessary, please read the link above):

<activityandroid:name=".activities.MainActivity"android:configChanges="orientation|screenSize"android:launchMode="singleTop" ><meta-dataandroid:name="android.app.searchable"android:resource="@xml/searchable" /><intent-filter><actionandroid: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)

publicbooleanonCreateOptionsMenu(Menu menu) {
    this.menu = menu;
    MenuInflatermenuInflater= getMenuInflater();
    menuInflater.inflate(R.menu.main_action_bar_menu, menu);

    MenuItemsearchItem= menu.findItem(R.id.action_search);
    searchView = (SearchView) searchItem.getActionView();
    searchView.setQueryHint(getResources().getString(R.string.hint));

    SearchManagersearchManager= (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
}

And finally override onNewIntent():

@OverrideprotectedvoidonNewIntent(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.

Solution 2:

Please check in your code if you didn't call:

 searchView.setIconifiedByDefault(true)

By removing it you should see the 'voice' icon.

Solution 3:

just put the android:launchMode="singleTop" in your Manifest's activity tag

<activityandroid:name=".Activity.MainActivity"android:launchMode="singleTop"
            >
          ....
            <intent-filter><actionandroid:name="android.intent.action.SEARCH"></action></intent-filter><meta-dataandroid:name="android.app.default_searchable"android:value=".Activity.MainActivity"
                ></meta-data><meta-dataandroid:name="android.app.searchable"android:resource="@xml/searchable"></meta-data></activity>

Post a Comment for "Actionbar Mic Button(voice Search) In Searchview"