Android-floating search box (searchmanager)

Source: Internet
Author: User

The use of the floating search box is not difficult, but because its configuration is very cumbersome. It is mainly used to facilitate developers to better design the UI when there is a search service in the program.

Follow these steps to use searchmanager:

(1) configure the search bar and create a searchable. xml configuration file under Res/XML, such as the default value, whether there are search recommendations or voice searches.

Code

 <Searchable xmlns: Android = http://schemas.android.com/apk/res/android <! -- Label is the text above the search box. The prompt text in the hint search box displays the label -->
Android: Label = "@ string/search_label"
Android: hint = "@ string/search_hint"
Android: searchmode = "showsearchlabelasbadge"
<! -- Voice Search configuration -->
Android: voicesearchmode = "showvoicesearchbutton | launchrecognizer"
Android: voicelanguagemodel = "free_form"
Android: voiceprompttext = "@ string/search_invoke" <! -- It is recommended to configure the search. The configuration error will not be displayed. The value of searchsuggestauthority here must be the complete path name inherited from Tranquility --> Android: searchsuggestauthority = "com. Android. cbin. searchsuggestionsampleprovider"
Android: searchsuggestselection = "? "
/>

(2) In manifest. xml configuration, there are two scenarios for the activity to be processed by the search result. One is to open an activtiy from the search bar in other activities.

Specifically, search results are processed. The second is that the current activity is the activity that processes the results. First, we will introduce the First Configuration:

Code

 <Activity Android: Name = "searchresultactivity">

<Intent-filter>
<Action Android: Name = "android. Intent. Action. Search"> </Action>
</Intent-filter> <! -- Specify the searchable. XML file --> <meta-data Android: Resource = "@ XML/searchable" Android: Name = "android. app. searchable "> </meta-data>

</Activity>

 

 
<! -- To make every activity use the search bar, you must put this label in the startup activity, and the value in it specifies
The previous search result activity -->
  <meta-data android:name="android.app.default_searchable"
android:value=".SearchResultActivity" />

(3) Recommended configuration in manifest. XML for search

 <! -- Previously searchable. the value of searchsuggestauthority in XML is actually directed to the searchsuggestionsampleprovider associated with name. It is a subclass of Tranquility --> <provider Android: Name = "searchsuggestionsampleprovider" Android: authorities = "com. android. cbin. searchsuggestionsampleprovider "> </provider>

 

The above authorities point to the searchsuggestionsampleprovider associated with the name, which is a subclass code of searchrecentsuggestionsprovider

 public class SearchSuggestionSampleProvider extends
SearchRecentSuggestionsProvider {

final static String AUTHORITY="com.android.cbin.SearchSuggestionSampleProvider";
final static int MODE=DATABASE_MODE_QUERIES;

public SearchSuggestionSampleProvider(){
super();
setupSuggestions(AUTHORITY, MODE);
}
}

 

 

(4) to use the search bar, we must override the onsearchrequested method of the activity to start a search bar on the interface.

However, this action is not automatically triggered and must be triggered by a button or menu click event;

Code

 @ Override
Public Boolean onsearchrequested (){

String text = etdata. gettext (). tostring ();
Bundle bundle = new bundle ();
Bundle. putstring ("data", text );

// Open the floating search box (the value of the first parameter added to the search box by default)
// Bundle is the transmitted data
Startsearch ("mm", false, bundle, false );
// The returned result must be true. If only the super. onsearchrequested method is used, not only // onsearchrequested (default value in the search box) cannot be added to the search box, but bundle cannot be passed out.
Return true;
}

(5) receive the query and bundle, and save the query value (that is, search for the recommended list value)

Code

 Public void dosearchquery (){
Final intent = getintent ();
// Obtain the value in the search box
String query = intent. getstringextra (searchmanager. query );
Tvquery. settext (query );
// Save search records
Searchrecentsuggestions suggestions = new searchrecentsuggestions (this,
Searchsuggestionsampleprovider. Authority, searchsuggestionsampleprovider. mode );
Suggestions. saverecentquery (query, null );
If (intent. action_search.equals (intent. getaction ())){
// Obtain transmitted data
Bundle bundled = intent. getbundleextra (searchmanager. app_data );
If (bundled! = NULL ){
String ttdata = bundled. getstring ("data ");
Tvdata. settext (ttdata );

} Else {
Tvdata. settext ("no data ");
}
}
}

Previously, we mentioned two possible scenarios for the activity processing results. Now we can handle the second situation, that is, if the invoke search bar's

Activity is also an activity that processes the search results. If you process the results in the previous way, an activity will be instantiated once you search for the results.

Key, you will find that the activity is always the same. In fact, in order to make it only have one instantiated object, you only need simple configuration and code to implement

First, add the Android: launchmode = "singletop" attribute to manifest. XML that processes the search result activity.

Second, rewrite the onnewintent (intent) of the activity)

Code

 @ Override
Public void onnewintent (intent ){
Super. onnewintent (intent );
// Obtain the value in the search box
String query = intent. getstringextra (searchmanager. query );
Tvquery. settext (query );
// Save search records
Searchrecentsuggestions suggestions = new searchrecentsuggestions (this,
Searchsuggestionsampleprovider. Authority, searchsuggestionsampleprovider. mode );
Suggestions. saverecentquery (query, null );
If (intent. action_search.equals (intent. getaction ())){
// Obtain transmitted data
Bundle bundled = intent. getbundleextra (searchmanager. app_data );
If (bundled! = NULL ){
String ttdata = bundled. getstring ("data ");
Tvdata. settext (ttdata );

} Else {
Tvdata. settext ("no data ");
}
}
}

Related Knowledge: the previous section mentioned how to add the latest search value to the search suggestion, but did not mention that if the value in the search suggestion is cleared, similar to saving, the searchrecentsuggestion object provides a clearhistory () method

 

Code

     private void clearSearchHistory() {
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);
suggestions.clearHistory();
}

Forgot: oye

 

 

 

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.