Reputation: 1957
the Android developer example shows us the way to use a searchView. It strongly suggests the receiver activity to declare an intent-filter for <action android:name="android.intent.action.SEARCH" />
in the manifest.
Is there a way to use another mechanism which won't require using and parsing intents?
Upvotes: 0
Views: 765
Reputation: 1957
Yes there is an option.
The android framework let you implement your own class from SearchView.OnQueryTextListener
and place a listener to control what will happen.
This class has 2 methods:
/** (not official documentation)
called when the text in the search's EditText is changing
*/
public boolean onQueryTextChange(String newText)
/** (not official documentation)
called when the search is submitted. This can happen in 3 ways:
@ the user presses the menu-item icon after inputting the text
@ the user presses his\her "enter" key in their keyboard
@ the developer dynamically called searchView.setQuery(CharSequence query, true)
*/
public boolean onQueryTextSubmit(String query)
Both of those method should return a boolean to indicate that they consumed the action and no other action should be done from the OS's side. Looking at the source code only onQueryTextSubmit() returned values is checked but for your own peace of mind just return true
in both methods.
complete code showing the listener in action:
manifest:
Don't add anything
searchable configuration:
Need to create such xml file.
right-click your res
folder and choose new --> android resource file
. put whatever you want as file name ("searchable" will work ok, for example), and choose XML
as resource type.
Then copy & paste this code in the created file (replace the hint string with your own):
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="put here your hint string to be shown"
/>
menu.xml:
<item
android:id="@+id/searchContacts"
android:icon="@drawable/ic_search_white_24dp"
android:title="search"
app:showAsAction="collapseActionView|always"
app:actionViewClass="android.widget.SearchView"
/>
<!-- and of course your other menu items -->
MainActivity:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu, menu);
implementSearch(menu);
return true;
}
private void implementSearch(final Menu menu) {
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
final MenuItem searchMenuItem = menu.findItem(R.id.searchContacts);
final SearchView searchView = (SearchView) searchMenuItem.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
@Override
public boolean onQueryTextChange(String newText){
// do whatever you want with this changed text
/* ... */
return true; // signal that we consumed this event
}
@Override
public boolean onQueryTextSubmit(String query){
// do whatever you want with this submitted query
/* ... */
return true; // signal that we consumed this event
}
});
}
Notice that more problems will may arise when adding a searchView to your menu items.
For an integrated fix to one of those problems (I've encountered exactly those) -
please refer to this SO answer.
Upvotes: 1