Devesh Agrawal
Devesh Agrawal

Reputation: 9212

widget.SearchView not showing MIC button for voice search in android

I have a seachview in layout file named activity_products_final

    <android.support.v7.widget.SearchView
        android:id="@+id/searchView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:queryHint="Search..">
    </android.support.v7.widget.SearchView>

</LinearLayout>

Activity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_products_final);



    searchView = (android.support.v7.widget.SearchView) findViewById(R.id.searchView);
    searchView.setIconified(false);
    searchView.onActionViewExpanded();
    searchView.clearFocus();
    searchView.setOnQueryTextListener(new android.support.v7.widget.SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            if (adapter != null)
                filterData(query);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String query) {
            if (adapter != null)
                filterData(query);
            return false;
        }
    });



    ImageView closeButton = (ImageView) searchView.findViewById(R.id.search_close_btn);
    closeButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            searchView.setQuery("", false);
            searchView.clearFocus();
            //collapseAll();
        }
    });
}

manifest file

<activity

    android:name=".ActivityProductList"
    android:configChanges="orientation|screenSize"
    android:label="@string/title_activity_products"
    android:launchMode="singleTop"
    android:parentActivityName=".ActivityStartShopping"
    android:screenOrientation="portrait"
    >
    <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
    </intent-filter>

    <meta-data
        android:name="android.app.searchable"
        android:resource="@xml/searchable" />

</activity>

xml/searchable

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

As per google docs i have to use android:voiceSearchMode="showVoiceSearchButton|launchRecognizer" to show MIC button.

I followed the doc but it is not showing MIC button to capture voice input.

Can anyone help me on this?

Upvotes: 8

Views: 6048

Answers (5)

aaronvargas
aaronvargas

Reputation: 14152

you need to set the search configuration to the SearchView

// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

Then you need to handle the Search results onNewIntent()

protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);

        searchView.setQuery(query, false);
    }
}

Upvotes: 2

Harish Jose
Harish Jose

Reputation: 326

I searched for the solution for this problem and finally I got the solution!!!

// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = findViewById(R.id.searchView);

// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); 
// Do not iconify the widget; expand it by default

You have to attach the SEARCH_SERVICE to your search view to enable the audio search

Upvotes: 1

TheMatrix
TheMatrix

Reputation: 91

You need to create a SearchableConfiguration. Add this as searchable.xml in your xml resource folder:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
  android:hint="search..."
  android:label="@string/app_name"
  <!-- the next line enables the voice button display -->  
  android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"/>

Upvotes: 4

Boycott A.I.
Boycott A.I.

Reputation: 18871

I have a similar issue - the mic icon shoes on some devices, but not on others (eg, Samsung Galaxy S5 device).

I have checked that this isn't an issue with my code by confirming the same issue occurs with the YouTube app.

I would recommend doing a web search for how to enable voice input for the particular device - or maybe posting for help/guidance in an Android/Samsung/etc users forum.

Also, make sure you have the Google app installed on the device.

Upvotes: 1

Rifat
Rifat

Reputation: 1888

Your device or emulator should have google services installed.

For emulators that doesn't have G-apps like Genymotion then one should download google apps manually. Available from opengapps.org/, use theirs any bundle and drop on emulator window and flash and reboot. May solve this.

Upvotes: 0

Related Questions