Setting Android search widget options via Searchable xml config
I have a menu item uses the search widget as its actionViewClass
that looks like so:
<item android:id="@+id/menu_search"
android:title="Search"
android:icon="@drawable/ic_menu_search"
android:showAsAction="always"
android:actionViewClass=开发者_StackOverflow社区"android.widget.SearchView" />
It works fine, however I have seem mention on the android site docs of a searchable xml config file like this:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="string resource"
android:hint="string resource" />
How can I get the menu item search widget to use the above configuration?? I assume it is something to do with the SearchManager.setSearchableInfo
method, I am getting a handle to the search widget in my code like this:
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchItem = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchItem.setSearchableInfo(searchManager.getSearchableInfo(R.layout.searchable));
As you can see I want to do something like above with the getSearchableInfo
method and pass in the id of my searchable config file.
Can anyone help me out?
You need to create a SearchActivity, which process the search queries from the search widget and show the search results and also you need to declare it in the android manifest file.
An example of search activity:
public class SearchEngine extends Activity
{
TextView result;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.search_view);
result=(TextView)findViewById(R.id.textView1);
this.setDefaultKeyMode(Activity.DEFAULT_KEYS_SEARCH_LOCAL);
final Intent queryIntent = getIntent();
final String queryAction = queryIntent.getAction();
if(Intent.ACTION_SEARCH.equals(queryAction))
{
this.doSearchQuery(queryIntent);
}
else if (Intent.ACTION_VIEW.equals(queryAction))
{
this.doView(queryIntent);
}
else
{
Log.d("Intent", "Create intent NOT from search");
}
}
@Override
public void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
final String queryAction = intent.getAction();
if (Intent.ACTION_SEARCH.equals(queryAction))
{
this.doSearchQuery(intent);
}
else if (Intent.ACTION_VIEW.equals(queryAction))
{
this.doView(intent);
}
}
public void doSearchQuery(Intent intent)
{
String queryString = intent.getDataString(); // from suggestions
if (queryString == null)
{
queryString = intent.getStringExtra(SearchManager.QUERY);
// from search-bar
}
finish();
}
public void doView(Intent intent)
{
Log.e("action view"," suggestion ");
}
}
and in the manifest:
<activity android:name=".SearchActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
You need to put the search configuration file 'searchable.xml' in the folder res/xml . The searchable.xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="Search"
android:searchMode="showSearchLabelAsBadge"
android:searchSettingsDescription="Custom Suggestions"
android:queryAfterZeroResults="false"
android:searchSuggestAuthority="com.example.customsearch.CustomSearchSuggestion"
android:searchSuggestSelection=" ? "
android:searchSuggestIntentAction="android.intent.action.VIEW"
android:voiceSearchMode="showVoiceSearchButton"
>
</searchable>
And to provide custom search suggestions, you need to create a class which extends ContentProvider or SearchRecentSuggestionsProvider. For more details look here
Also in the activity which contains the search view, you need to set the searchable info as:
SearchManager searchManager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
Hope this helps you, Good luck!
精彩评论