[Android] webview built-in Search dialog box

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.