Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a SearchView have both voiceSearchButton and keyboard's voice input?

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?

like image 761
PLNech Avatar asked Nov 13 '17 16:11

PLNech


People also ask

How do I customize my SearchView widget?

just do your own search view, it is very simple. you then can include this layout in your activity layout file. This is a simple layout which includes a "search icon" followed by EditText, followed by "clear icon". The clear icon is shown after user types some text.

How to implement SearchView in Android?

SearchView widget can be implemented over ToolBar/ActionBar or inside a layout. SearchView is by default collapsible and set to be iconified using setIconifiedByDefault(true) method of SearchView class. For making search field visible, SearchView uses setIconifiedByDefault(false) method.

How do I set text in search view?

You can use setQuery() to change the text in the textbox. However, setQuery() method triggers the focus state of a search view, so a keyboard will show on the screen after this method has been invoked. To fix this problem, just call searchView.

How to make search button in Android?

Invoking the search dialog For instance, you should add a Search button in your Options Menu or UI layout that calls onSearchRequested() . For consistency with the Android system and other apps, you should label your button with the Android Search icon that's available from the Action Bar Icon Pack.


1 Answers

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"
like image 63
robertapengelly Avatar answered Oct 21 '22 05:10

robertapengelly