Content Search in android and android Content Search

Source: Internet
Author: User

Content Search in android and android Content Search

When writing android search code, there are two ways to implement the search function:

Custom Search methods:

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

2. Custom Voice Input Method

3. Customize common popular word content and styles

4. Custom browsing record content and style

5. Use the custom pop-up style in combination with the listView

6. Custom Data source formats and search algorithms

User-Defined search is more complex than custom search, so we need to think more.

Android built-in search methods:

1. Create a configuration file for the Search dialog box

2. Create an Acitivity for search and execute search

3. Accept search query and call the Search dialog box

The pop-up style of the custom algorithm is removed, and the data source format and algorithm are searched.

Android search method advantages:

1. When you need to provide the search service in your application, where do you first think of your search box? Use the Android 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 provides an application with a Custom Search dialog box that provides a standardized search experience and adds features such as voice search and search recommendations.

The search framework of Android manages the Search dialog box on your behalf. You do not need to develop a search box on your own. You do not need to worry about the location of the search box or the impact of the search box on your current interface. All these tasks are handled by the SearchManager class. SearchManager manages the entire lifecycle of the Android Search dialog box and returns the corresponding search keywords after your application sends a search request.

When a user executes a search term, SearchManager uses a specialized Intent to send the search keyword to the Activity configured in the configuration file to process the search result. In essence, all you need to prepare is an Activity to receive Intent, then perform the search and give the result. Specifically, you need to do the following:

(1) A search configuration, which uses an XML configuration file to configure the Search dialog box, including some function configurations, such as text boxes, set the prompt text displayed in the voice search and search suggestions. (Searchable. xml)

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

(3) The search path executed by a user. By default, once you configure a searchable Activity, the device search key (if any) will call the Search dialog box. Then, you should use another method to allow users to call the Search dialog box, such as the search button in the option menu and the buttons on other user interfaces, because not all devices provide a dedicated search key.


I. Search Configuration

The search box configuration file is an XML file used to configure the application. This file is generally named searchable. xml and must be saved in the res/xml/directory of the project. The root node of the configuration file must be searchable and can have one or more attributes.

<?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 preceding configuration file, except for the android: hint attribute, all other configuration items are required in a Search dialog box. android: label is a required attribute and its value is a string resource reference, you cannot use strings directly. Normally, the name of the application (although it is a required attribute, It is not displayed normally unless you have enabled the search suggestion function ). Android: hint is the input prompt for configuring the search box. It must also reference the string resource configured in string. xml and cannot be used directly. Many attributes can be configured, but most of the attributes are configured only when the search suggestion and Voice Search are used. However, you should configure android: hint as much as possible to indicate the information you need to enter.

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

2. Create an Activity for search

When a user performs a search from a search box, SearchManager sends the content to be searched (keywords) to an Activity that can be searched by ACTION_SEARCH Intent. This Activity queries data and displays results. Create an Activity for executing the search, declare that it can respond to ACTION_SEARCH Intent, and add search box configuration information. Therefore, you need to add an element and a node of an element in your manifest file. As follows:

<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>

In the code, the value of android: name must be "android. app. searchable ", android: resource attribute value must reference the search configuration file in the res/xml/directory mentioned above (res/xml/searchable in this article. xml ).

Note that only the Activity node configured with the above meta-data node can perform a search. If you want to call the search box throughout the application, you can perform 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 above Code, andorid: name = "android. app. default_searchable" defines the name of a search request in the response search box. android: value specifies the Activity that responds to the request and performs the search. When we execute a search request in the OtherActivity In the application, the SearchActivity will be loaded for executing the search and displaying the search results.

When an Activity is declared as searchable, the actual search involves three steps: accept the query, retrieve your data, and submit the results. Generally, your search results need to be displayed in a listView. Therefore, the Activity to be executed must inherit the ListActivity, so that you can access the ListView API.

When you perform a search in the Search dialog box, the configured Activity that can be used for the search will be activated by Intent, with some search-related parameters. You need to check the Intent and make a search response, as follows:

Intent intent = getIntent (); // determines whether the search request is if (Intent. ACTION_SEARCH.equals (intent. getAction () {// obtain the search content (keyword) String query = intent. getStringExtra (SearchManager. QUERY); // execute the corresponding query action doMySearch (QUERY );}

The doMySearch () method queries the database or data information from the network based on the keyword. If it is a time-consuming search, you also need to use the search progress bar to tell the user that the search is in progress, after the final result is returned, you can call the setAdapter () method of ListView to display the result in ListView.

You can call the onSearchRequested () method from any place in the application to activate the search box, such as a menu or a button. You can also call it in the OnCreate () method. Setdefakeykeymode (DEFAULT_KEYS_SEARCH_LOCAL). When you press the key on the keyboard, the search box is automatically activated.

The search box is the same as the normal dialog box. It floats at the top of the screen and does not change the Acitivity stack status. No methods in the Activity life cycle will be called. Only when the search box appears, A running Activity will lose the input focus.

If you want to perform other operations during the search, you can override the OnSearchRequested () method, as shown below:

@ Override public boolean onSearchRequested () {// do what you want in this method, for example, do something that has been first processed as pauseSomeStuff (); return super. onSearchRequested ();}

If the current Activity is the requested Activity, there are two situations:

By default, ACTION_SEARCH Intent will create a new Activity and call the OnCreate () method. This new Activity will display at the beginning, and you will have two Activity instances at the same time. When you press the "return" key, the Activity before the search is not executed will be returned.

Another scenario is the Activity with android: launchMode = "singleTop" configured. At this time, we need to process the search request in the onNewIntent (Intent) method, as shown below:

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 is as follows:
<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 parameters to the search box, we need to override the onSearchRequested () method, as shown below:

@Overridepublic boolean onSearchRequested(){     Bundle appData= new Bundle();     appData.putBoolean(MySearchableActivity.JARGON,true);     startSearch(null,false, appData,false);     return true;}

Finally, let's take a look at how to use android Voice Search:

You only need to make the following changes to our search configuration file. Your search supports Voice Search and configuration files.

<?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>







How to search txt text in Android?

I made a reader, but I still need to implement full-text search. I have never touched on the search code before, so I am not familiar with this. I wonder if anyone knows how to implement it, I want to implement full-text search for txt and epub e-books.
 
How does Android implement online search for content in EditText?

Start the browser with intent and pass the parameters. Assume that the EditText name is editText. In the listener of the OK button, Intent intent = new Intent (); intent. setAction (Intent. ACTION_VIEW); Uri uri = Uri. parse ("www.baidu.com/mongowd=" + editText. getText (); intent. setData (uri); startActivity (intent );

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.