Search for content in Android

Source: Internet
Author: User

When writing the Android search code, how to implement the search function, consider two kinds of:

Define your own Search method:

1. Define Search input box, search icon, search button

2. Define your own voice input method

3. Define yourself frequently using hot word content, styles

4. Define your browsing history content and style

5. Define pop-up styles yourself using Spinner with ListView

6. Define your own data source format and search algorithm

It's more complicated to define your own search than it is for us to think more.

Android comes with the search method:

1. Create a search dialog box configuration file

2. Create a acitivity and run search that can be used for searching

3. Accept the search query and invoke the Search dialog box

Omitted to define the algorithm's pop-up style, search data source format and algorithm two chunks.

Advantages of the Android search method:

1. When you need to provide search services in your application. Your first thought is where do you want your search box to be placed? Process a user's search request by using the Android Search dialog box. With a simple search button or calling an API from your application, the Search dialog box will appear at the top of the screen and will display your application icon on its own initiative.


This article implements an application that provides a self-defined search dialog box that provides a standardized search experience. and add features such as voice search and search suggestions.

The Android Search framework will manage the search dialog for you, and you won't need to develop a search box yourself. There's no need to worry about where the search box is located or what the search box does to affect your current interface. All of this work has a Searchmanager class to deal with. Searchmanager manages the entire life cycle of the Android Search dialog box. After your application sends a search request. Returns the corresponding search keyword.

When the user runs a search, Searchmanager will use a dedicated intent to route the keyword of the search query to the activity that you configured in the configuration file to process the search results.

Essentially, all you need to prepare is an activity to receive intent. Then run the search and give the results. In detail, the things you need to do include:

(1) A search configuration, using an XML configuration file to configure the Search dialog box. A configuration that contains some features, such as a text box. Set the prompt text that appears in speech search and search suggestions, and so on.

(Searchable.xml)

(2) An activity to process the search request. This activity is used to receive the content of the search query, then search your data and display the search results.

(3) A user-run search path, by default. Once you have configured a searchable activity, the device search key (assuming it exists) invokes the Search dialog box. Then, you should use the provide there is also a means for the user to be able to invoke the Search dialog box. As in the Options menu, Search button and other user interface buttons, since not all devices provide a dedicated search key.


First, the search configuration

The search box configuration file is an XML file that is used to configure the application. This file is generally named Searchable.xml. and must be saved under the Res/xml/folder of the project. The root node of the configuration file must be searchable and can have one or more properties.

<?xml version= "1.0" encoding= "Utf-8"? ><searchable xmlns:android= "http://schemas.android.com/apk/res/ Android "     android:label=" @string/app_name "    android:hint=" @string/app_version "></searchable>

In the above configuration file, in addition to the Android:hint property, the other is a search dialog box must be a configuration item, Android:label is a required property. Its value is a string resource reference and cannot be used directly with a string, typically returning the name of the application (although it is a mandatory attribute, it is usually not displayed.) Unless you turn on the search suggestions feature).

Android:hint is the input prompt for configuring the search box, and you must also reference the configured string resource in String.xml, and you cannot use the string directly. ability to configure very many properties. However, most properties are only configured with search suggestions and voice searches, although you can configure android:hint as much as possible to prompt the user for information that needs to be entered.

Next, you need to put this configuration file in your application.

Ii. Create an activity that can be used for search

When a user runs a search from a search box, Searchmanager sends the content to be searched (keyword) through action_search intent to an activity that can run the search.

The activity queries the data and displays the results. Create an activity to run the search, declaring that it responds to Action_search Intent. and add the search box configuration information. To do this, you need to add an element and an element to the node in your manifest file. For example, see the following:

<activity     android:name= "com.spring.mainview.SearchActivity"    android:theme= "@style/ Default.notitlebar ">    <intent-filter>        <action android:name=" Android.intent.action.SEARCH "/ >    </intent-filter>    <meta-data android:name= "android.app.searchable"        android:resource= "@ Xml/searchable "/></activity>

The Android:name attribute value in the code must be "Android.app.searchable". The Android:resource property value must refer to the search configuration file under the Res/xml/folder mentioned above (Res/xml/searchable.xml in this article).

Note that the ability to run a search is only possible with the activity node configured with the Meta-data node above, and the false assumption is that the search box can be invoked throughout the application, such as the following configuration:

<activity     android:name= "com.spring.mainview.SearchActivity"    android:theme= "@style/ Default.notitlebar ">    <intent-filter>        <action android:name=" Android.intent.action.SEARCH "/ >    </intent-filter>    <meta-data android:name= "android.app.searchable"        android:resource= "@ Xml/searchable "/></activity><meta-data android:name=" android.app.default_searchable "    android: Value= "Com.spring.mainview.SearchActivity"/>

In the code above, andorid:name= "Android.app.default_searchable" defines a name for the search request that responds to the search box, android:value which activity responds and runs the search.

When we run a search request in Otheractivity in the application, Searchactivity will be loaded to run the search and display the search results.

When an activity is declared searchable. Running an actual search consists of three steps: accepting the query, retrieving your data, and submitting the results.

Typically, your search results need to be displayed in a ListView. So the activity to be used to run is to inherit listactivity, so that the access to the ListView API can be asked.

When you run a search from the Search dialog box, the activity that you just configured for search will be activated by intent. At the same time with some search-related references, you need to check the intent and make a search response. For example, see the following:

Intent intent= getintent ();//Infer if the search request is (Intent.ACTION_SEARCH.equals (Intent.getaction ())) {// Gets the query content of the search (keyword) String query = Intent.getstringextra (searchmanager.query);//Run the corresponding query action domysearch (query);}

The Domysearch () method will query the database based on keyword or query the data from the network, assuming that it is time-consuming search, you also need to use the search progress bar to tell the user that the search is in progress, and finally return the results, can call the ListView Setadapter () method to display the results in the ListView.

You can invoke the Onsearchrequested () method from anywhere in the application to activate the search box, for example, from a menu or a button.

You can also invoke it in the OnCreate () method. Setdefaultkeymode (default_keys_search_local), so that when the user presses a key on the keyboard, the search box is actively activated.

The search box floats at the top of the screen just like a normal dialog box. He does not change the acitivity stack state. No matter what the method in the activity life cycle is called, only the activity that is being executed loses the input focus when the search box appears.

Suppose you want to run a search and do something else. Ability to override the Onsearchrequested () method. For example, the following can be seen

@Override Public    Boolean onsearchrequested () {    //Do what you want to do in this way, for example, to do some work        pausesomestuff ();        return super.onsearchrequested ();    }

Assuming that the current activity is the activity that corresponds to the search request, there are two scenarios:

By default. Action_search Intent will create a new activity and call the OnCreate () method. This new activity will be displayed in the front. You will have two instances of activity at the same time.

When you press the "Back" button, you go back to the activity before you ran the search.

There is another situation where the activity of android:launchmode= "Singletop" is configured, at this time. We need to process the search request in the Onnewintent (Intent) method, as seen in the following:

Code

@Overridepublic void OnCreate (Bundle savedinstancestate) {    super.oncreate (savedinstancestate);    Setcontentview (r.layout.search);    Handleintent (Getintent ());} @Overrideprotected 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);      Domysearch (query);}    }

the corresponding activity configuration such as the following
<activity android:name= ". Mysearchableactivity "android:launchmode=" Singletop "><intent-filter>  <action android:name=" Android.intent.action.SEARCH "/></intent-filter><meta-data android:name=" android.app.searchable " Android:resource= "@xml/searchable"/></activity>

To pass the parameters to the search box, we need to rewrite the onsearchrequested () method. For example, see the following:

@Overridepublic Boolean onsearchrequested () {     bundle appdata= new Bundle ();     Appdata.putboolean (mysearchableactivity.jargon,true);     Startsearch (Null,false, appdata,false);     return true;}

Finally, let's look at how to use Android's voice search:

Just need to make a change to our search configuration file for example the following. Your search is supported for voice search. Configuration file

<?

XML version= "1.0" encoding= "Utf-8"? ><searchable xmlns:android= "Http://schemas.android.com/apk/res/android" android:label= "@string/app_name" android:hint= "@string/app_version" android:voicesearchmode= " Showvoicesearchbutton|launchrecognizer "></searchable>






Search for content in Android

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.