Reputation: 1012
After hours of trying to get a support SearchView in my support Toolbar to work correctly (it shows up, shows the hint text but refuses to accept text), I have to turn to you.
I have Googled for hours, read A LOT of answers here and in other sites, checked out a lot of documentation and a specific blog post dealing with the subject but I just can't isolate the issue.
As I have said, I am using the support versions of SearchView and Toolbar and also MenuItemCompat. Also, ActionBarActivity. I made sure that I followed all the steps religiously (this blog post had a lot to say on the subject, even mistakes in the official documentation).
I have set the AndroidManifest.xml file correctly, I have a searchable.xml which, as far as I understand, is referenced correctly (I mean, the hint text shows up normally in the SearchView field), I have a search result activity with an android.intent.action.SEARCH intent filter set in the manifest.
Still nothing. I'm honestly stumped here.
Here's the onCreateOptionsMenu() of the SearchView-hosting activity:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem search = menu.findItem(R.id.search);
searchView = (SearchView) MenuItemCompat.getActionView(search);
SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
return true;
}
And here is searchable.xml in res/xml:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/search_hint"
android:label="@string/search_title" >
</searchable>
Finally, here is the menu.xml file in res/menu:
<item
android:id="@+id/search"
android:icon="@drawable/ic_search_white_18dp"
android:title="@string/search_title"
myappname:actionViewClass="android.support.v7.widget.SearchView"
myappname:showAsAction="ifRoom|collapseActionView"/>
(obviously myappname replaces my real app name here).
Weirdly, voice search works perfectly normal after I add
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
in searchable.xml. It even sends the recognised phrase in the intent to the search results activity. The only problem is that I simply can't input text in the text field - there's not even a cursor blinking there. Attempts to do something like searchView.requestFocus() have no result.
Upvotes: 4
Views: 2415
Reputation: 1012
After wasting 8 hours on this, I came up with a solution. I'm not sure about how much of a hack it is but it works.
I noticed that the EditText in the SearchView was behaving erratically as far as focusing was concerned: it had no cursor and, weirdly enough, when I pulled the notification drawer down, it seemed to change focus. I decided to go with the theory that the problem was the focus of the EditText inside the SearchView, so I went looking for the android.R.id of the EditText.
After searching, I came upon this answer. So I got a reference to the EditText within the SearchView thusly:
EditText searchField = (EditText)
searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
I then requested focus for the EditText:
searchField.requestFocus();
... and suddenly the SearchView could accept text, with all the relevant callbacks working!
I only had one problem left: when onPause() was called, my solution stopped working since the EditText lost focus. So I just added:
invalidateOptionsMenu();
...in onResume() and now it works perfectly.
I must say, though: it was a really weird experience since I followed the steps religiously (not in the official Android developer guide, which has a number of mistakes) and still couldn't get the SearchView EditText to focus. I have a suspicion that the nav drawer has the focus upon the Activity creation and maybe that was the source of the problem but I'm not sure yet.
Upvotes: 2