5

I'm adding voice search capabilities to my app's search interface. Currently, it looks like this:

initial search interface

To add voice search, I added a voiceSearchMode to my Searchable Configuration, which adds a button in the SearchView to trigger the voice dialog. The interface now looks like this:

enter image description here

However as you can see on the keyboard's , key, the microphone button is now disabled. I can't find any documentation on how to turn it on again, and the only related questions explain how to explicitly disable it.

Is it possible to have both the system's voice search dialog and the keyboard's voice input in a SearchView?

PLNech
  • 3,087
  • 1
  • 23
  • 52

1 Answers1

1

I know this is an old question but couldn't find an answer through Google so thought I'd add here what I found. After taking a look at the SearchView's source code I found the following code in setSearchableInfo method:

if (mVoiceButtonEnabled) {
  // Disable the microphone on the keyboard, as a mic is displayed near the text box
  // TODO: use imeOptions to disable voice input when the new API will be available
  mSearchSrcTextView.setPrivateImeOptions(IME_OPTION_NO_MICROPHONE);
}

What I then did was to create a class that extends SearchView, override the setSearchableInfo method and after calling super.setSearchableInfo add the following:

SearchAutoComplete mSearchSrcTextView = findViewById(android.support.v7.appcompat.R.id.search_src_text);
mSearchSrcTextView.setPrivateImeOptions("");

Now I have a microphone on the keyboard and one in the SearchView.

Edit: Don't forget to reference your class if you're using it in the menu.

app:actionViewClass="your.searchview.class"
Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
  • Hi @robertapengelly, thanks for your reply! This indeed works, although using undocumented API that might break in future releases (nothing guarantees that `mSearchSrcTextView` will still be there / called this way in next releases). Still, it's the best option so far! – PLNech Apr 03 '19 at 15:14