Android Getting Started: Build the android built-in Search dialog box on Google map (floating search)

Source: Internet
Author: User

In the map application, a floating search box is often used to search for nearby poi information, and these functions are basically similar. So I checked them online and found them in the SDK documentation in Dev. I have provided a detailed introduction in the Guide, but I have read it in English for a long time.

 

The function is relatively simple, that is, it is a bit difficult to configure.

 

First, check the effect.

 

 

The configuration is as follows:

 

I,Search box configuration fileIs an XML file used to configure the settings in the search box in your application. This file is generally named searchable. xml and 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.

 

In searchableinfo, Android obtains the configuration file through COM. Android. Internal. R. styleable. searchable.

 

This searchable should be the name of the tag, so it must be named like this. The file name is not important.res/xml/Project directory is not important to this file name, but the file must be placed in the XML directory.<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <searchable xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Label = "@ string/searchlabel" Android: hint = "@ string/searchhint" <br/> Android: icon = "@ drawable/menu_route" Android: searchsuggestauthority = "com. debian. googleMap. suggestionprovider "<br/> Android: queryafterzeroresults =" false "Android: searchsuggestselection = "? "> <Br/> </searchable>

 

Here is an android: icon = "@ drawable/menu_route". I thought it was possible to search for the view in front of text and then found that it did not work, this attribute is not mentioned in the document. It seems useless, because it may take a long time for me to talk about it later.

 

Note that the Android: Label Android: hint attribute cannot directly write values, but points to a stringresource is configured in values.

Android: Label label does not know what to use, but Android: hint is required. When textview is empty, the displayed value is equivalent to the prompt information.

 

There are also a large number of basic configurations for voice search, but it is estimated that this function is not very commonly used. If you want to study it, please refer to the documentation for details.

 

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <searchable xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Label = "string resource" <br/> Android: hint = "string resource" <br/> Android: searchmode = ["queryrewritefromdata" | "queryrewritefromtext"] <br/> Android: searchbuttontext = "string resource" <br/> Android: inputtype = "inputtype" <br/> Android: imeoptions = "imeoptions" <br/> Android: searchsuggestauthority = "string" <br/> Android: searchsuggestpath = "string" <br/> Android: searchsuggestselection = "string" <br/> Android: searchsuggestintentaction = "string" <br/> Android: searchsuggestintentdata = "string" <br/> Android: searchsuggestthreshold = "int" <br/> Android: includeinglobalsearch = ["true" | "false"] <br/> Android: searchsettingsdescription = "string resource" <br/> Android: queryafterzeroresults = ["true" | "false"] <br/> Android: voicesearchmode = ["showvoicesearchbutton" | "launchwebsearch" | "launchrecognizer"] <br/> Android: voicelanguagemodel = ["free-form" | "web_search"] <br/> Android: voiceprompttext = "string resource" <br/> Android: voicelanguage = "string" <br/> Android: voicemaxresults = "int" <br/> <actionkey <br/> Android: keycode = "keycode" <br/> Android: queryactionmsg = "string" <br/> Android: suggestactionmsg = "string" <br/> Android: suggestactionmsgcolumn = "string"> <br/> </searchable> <br/>

 

 

 

II,Create a Search Activity

 

Here, the activity can be created. Of course, it can also be the acitvity in the pop-up search box. Here, mapacitivity is used to display the search results. So here we use this acitivity directly.

The implementation of these two methods is similar, but there is also a small difference below:

First, the configuration is the same.

<Intent-filter> <br/> <action Android: Name = "android. intent. action. search "/> <br/> </intent-filter> <br/> <meta-data Android: Name =" android. app. searchable "<br/> Android: Resource =" @ XML/searchable "/>

 

That is, the search function can be used only in this activity. In fact, the Android search box supports the entire application.

In this way, you need to create an acitivity dedicated to search. You can configure the acitivity under the <Application> </Application> label.

<! -- Declare the default searchable activity for the whole app --> <br/> <meta-data Android: Name = "android. app. default_searchable "<br/> Android: value = ". mysearchableactivity "/> <br/>

 

III,Call the search boxNow that the configuration is complete, you can start to call

 

The method to call is very simple. All the acitiers can call the onsearchrequested () method so that the search box will appear, so there can be a simple method during testing.

Call setdefakeykeymode (default_keys_search_local) in the oncreate () method. In this way, when you press the key on the keyboard, the search box is automatically activated.

If you want to perform other operations during the search, you can rewrite the onsearchrequested () method as follows:

@ Override <br/> Public Boolean onsearchrequested () {<br/> // do what you want in this method <br/> dosometingother (); <br/> return Super. onsearchrequested (); <br/>}

 

Also, if we want to pass some parameters during the call

 

Public Boolean onsearchrequested () {<br/> log. I (TAG, "onsearchrequested ------------ ========"); <br/> bundle appdata = new bundle (); <br/> appdata. putstring ("key", "your info"); <br/> startsearch (null, true, appdata, false); <br/> return true; <br/>}

 

IV,Accept query conditions and execute Query

 

If you have created an acitivity that specifically handles the query, you can directly perform the query operation in oncreate.

@ Override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. search); <br/> intent = getintent (); <br/> If (intent. action_search.equals (intent. getaction () {<br/> string query = intent. getstringextra (searchmanager. query); <br/> domysearch (query); <br/>}< br/>}

 

However, if it is on the current acitivity, it cannot be executed once for oncreate. This can be achieved through onnewintent.

@ Override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. search); <br/> handleintent (getintent (); <br/>}</P> <p> @ override <br/> protected void onnewintent (intent) {<br/> setintent (intent); <br/> handleintent (intent); <br/>}</P> <p> private void handleintent (intent) {<br/> If (intent. action_search.equals (intent. getaction () {<br/> string query = intent. getstringextra (searchmanager. query); <br/> domysearch (query); <br/>}< br/>}

 

 

In this way, the query operation will be completed through domysearch (). However, after the query is complete, you need to note that after the query is complete, I find that the acitivity is the one before the query.

 

This indicates that there are two of my mapacitivity instances in the activity stack, which can be solved through configuration like Android: launchmode = "singletop" in acitivity.

 

My acitivity configuration is like this.

 

<Activity Android: Name = ". googlemapactivity "Android: launchmode =" singletop "<br/> Android: label = "@ string/app_name"> <br/> <intent-filter> <br/> <action Android: Name = "android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> <action Android: Name =" android. intent. action. search "/> <br/> </intent-filter> <br/> <meta-data Android: Name =" android. app. searchable "<br/> Android: Resource =" @ XML/searchable "/> <br/> </activity>

 

 

V,Record history keywordsAfter the query is complete, we want to save the query conditions and even some linked results.

 

Android implements the function of saving keywords through searchrecentsuggestionsprovider.

 

First create a provider class

 

Public class searchsuggestionprovider extends searchrecentsuggestionsprovider {<br/>/** <br/> * authority <br/> */<br/> final static string authority = "com. debian. googleMap. suggestionprovider "; <br/>/** <br/> * mode <br/> */<br/> final static int mode = database_mode_queries; <br/> Public searchsuggestionprovider () {<br/> super (); <br/> setupsuggestions (authority, mode); <br/>}< br/>}

 

 

Of course, you need to configure it in manifest.

<Provider Android: Name = "com. delobby. GoogleMap. searchsuggestionprovider"
Android: Authorities = "com. delobby. GoogleMap. suggestionprovider"/><Provider Android: Name = "com. delobby. GoogleMap. searchsuggestionprovider" <br/> Android: Authorities = "com. delobby. GoogleMap. suggestionprovider"/>

 

Note that the configuration of Android: authorities is consistent with that in provider.

 

In this way, you can call

 

 

Inclusuggestions = new searchrecentsuggestions (this, <br/> searchsuggestionprovider. Authority, searchsuggestionprovider. mode); <br/> suggestions. saverecentquery (query, null );

 

After saving, click search. After the search is completed, the result is saved successfully. The next search will show the effect pp.

 

 

 

In some cases, you need to save some query results. For example, if you want to query a location on a map, you can quickly query the location in the next query.


In this case, you can save the information of the last queried location, such as the longitude and latitude, so that you can manually Save the data through sqlit.

 

You can perform the insert query operation in the handleintent () method, that is, database operations are not implemented in detail.

 

Now, let's continue to study the icon problem.

 


:
Click to download

 

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.