I've got a SearchView
that doesn't seem to be working. The SearchView
shows in the menu bar, and I can enter text. However when I click the search icon on my phone's on-screen keyboard, nothing at all happens. Ideally, this would start a new activity, sending along a query string to be used. I tried to implement the SearchView.setOnSearchClickListener
method to handle the click, but when I do that, the result activity loads as soon as I click the search icon to enter text, with no parameters. Any help at all pinpointing what I might be doing wrong would be greatly appreciated.
I create the menu like so in my Activity:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
final SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.convert:
...
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And the xml for the search:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/search"
android:title="@string/search"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="always"
android:actionViewClass="android.support.v7.widget.SearchView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
<item android:id="@+id/convert"
android:title="@string/dlconvert" />
</menu>
I've also added this to the manifest in the activity where the search shows:
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable"/>
And this is the definition of the search result activity:
<activity android:name=".ActivitySearchResults">
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable"/>
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
</intent-filter>
</activity>
The searchable.xml file just contains:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="Search..." />
EDIT - the problem is not the same as the possible duplicate. The activity that is supposed to receive the intent never starts at all.
Try
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);
SearchView.SearchAutoComplete searchAutoComplete = searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchAutoComplete.setHintTextColor(Color.GRAY);
searchAutoComplete.setTextColor(Color.BLACK);
return true;
}
than implements SearchView.OnQueryTextListener
to your activity ans Override methods
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Intent intent = new Intent(Activity.this, SearchActivity.class);
intent.putExtra("query", query);
startActivity(intent);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});