When you need to provide the search service in your application, where do you first think of your search box? By using the Android search framework, the application will display a Custom Search dialog box to process users' search requests. By using a simple search button or calling an API from your application, the Search dialog box is displayed at the top of the screen and your application icon is automatically displayed.
This article will teach you how to provide a Custom Search dialog box for your application. In this way, you can provide users with a standardized search experience and add functions such as voice search and search recommendations.
Procedure:
1. androidmanifest. xml configuration
<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "tianshuai. UC "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <application Android: Label =" @ string/app_name "Android: icon = "@ drawable/icon"> <activity Android: Name = "mainactivity" Android: Label = "@ string/app_name" Android: launchmode = "singletop"> // you do not need to call the search box to execute oncreat every time. It is troublesome to create an activity so that it is completely disabled <intent-filter> <action Android: Name = "android. intent. action. main "/> <category Android: Name =" android. intent. category. launcher "/> </intent-filter> <action Android: Name =" android. intent. action. search "/> <category Android: Name =" android. intent. category. default "/> </intent-filter> // configuration file <meta-data Android: Name =" android. app. searchable "Android: Resource =" @ layout/searchable "/> </activity> // specify the corresponding activity class <meta-data Android: Name =" android. app. default_searchable "Android: value = ". mainactivity "/> </Application> <uses-Permission Android: Name =" android. permission. internet "/> </manifest>
Ii. layout/searchable. xml
<?xml version="1.0" encoding="utf-8"?><searchable xmlns:android="http://schemas.android.com/apk/res/android" android:label="@string/searchLabel" android:hint="@string/searchHint" android:icon="@drawable/horse" android:searchSuggestAuthority="com.debby.googlemap.SuggestionProvider" android:queryAfterZeroResults="false" android:searchSuggestSelection=" ? "> </searchable>
3. mainactivity. Java
@Override protected void onNewIntent(Intent intent) { setIntent(intent); handleIntent(intent); } private void handleIntent(Intent intent) { if (Intent.ACTION_SEARCH.equals(intent.getAction())) { String query = intent.getStringExtra(SearchManager.QUERY); if(!query.startsWith("http://")){ mWebView.loadUrl("http://"+query); } if(query.startsWith("http://")){ mWebView.loadUrl(query); } } }
Public Boolean onkeydown (INT keycode, keyevent event) {// mobile phone buttons // Search button if (keycode = keyevent. keycode_search) {// call the search box onsearchrequested ();}
public boolean onSearchRequested(){ this.startSearch(null, false, null, false); return true; }
If you do not understand or make any mistakes, you are welcome to criticize and correct them.