Basic ways to use Android search controls

Source: Internet
Author: User

In Android, search is a very central feature that we can use to search for any information we can get. This information can be information that is stored on the phone, such as contacts, files, or resources on the network.

In order to provide users with a good search experience, Android has deliberately provided a search framework to facilitate developers to integrate search functions in their own apps. Then I'll take a look at this search framework.

The Android Search framework provides us with two types of UI controls for search, namely:

    • Search Dialog

Search dialog is a system-managed searching control that floats above the activity. It does not participate in the activity's life cycle.

    • Search Widget

Search Wiget This is a editview-like control that you can place anywhere you want.

Here we mainly study the use of Search dialog, the use of search widget readers can be in http://developer.android.com/guide/topics/search/ Index.html study, have any problem also can discuss with me.

To use search Dialog, we need to prepare three things:

    1. A searchable configuration this is an XML config file searchable.xml and put it under Res/xml
    2. A searchable activity this is an activity that is used to process user-entered search keywords
    3. A search interface This is the interface that is provided by a searching control, and for search dialog it provides an interface that is onsearchrequested

Based on the three points above, we started to write a simple app that includes a search function

1. Writing searchable configuration

The contents of the Searchable.xml file are as follows and put it under the res/xml of the project

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

App_label generally specifies the name of the app, App_label is not visible when using the simple search function, and will only appear in the Quick Search box's searchable list when you add your search function to the Quick Searches box.

Search_hint is when we don't have any input strings that appear on search dialog

2. Create a searchable Activity

Assuming the name of the activity we created is searchableactivity, then the configuration in Androidmanifest is as follows:

<Application... >    <ActivityAndroid:name=". Searchableactivity " >        <Intent-filter>            <ActionAndroid:name= "Android.intent.action.SEARCH" />        </Intent-filter>        <Meta-dataAndroid:name= "Android.app.searchable"Android:resource= "@xml/searchable"/>    </Activity>    ...</Application>

One of the android:name= "Android.app.searchable" tells the Android system that searchableactivity this sector faces Android.intent.action.SEARCH sensitivity.

Android:resource= "@xml/searchable" is to specify the configuration file that we have in the first step.

3. Call the search interface and process user input

We've only defined an interface that handles search keywords, and then we'll define an interface to invoke the search interface:

For the sake of simplicity, I just added a button to the app's first interface Mainactiviy, and then called the search dialog through the button.

Mainactiviy.oncreate

    Private button button;    @Override    protectedvoid  onCreate (Bundle savedinstancestate) {        super  . OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);         = (Button) Findviewbyid (R.id.button);        Button.setonclicklistener (new  View.onclicklistener () {            @Override              publicvoid  OnClick (View v) {                onsearchrequested ();//search dialog's Calling interface            }        });    

Mainactiviy corresponding Androidmanifest configuration

       <ActivityAndroid:name=". Mainactivity "Android:label= "@string/app_name" >            <Meta-dataAndroid:name= "Android.app.default_searchable"Android:value=". Searchableactivity " />            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>

which

<meta-data android:name= "android.app.default_searchable" android:value= ". Searchableactivity "/> Specifies the right of Mainactivity to invoke Search dialog, and specifies the discovery processing interface searchableactivity.

Here, when you click on the button on the Mainactivity, onsearchrequested () is called to activate the search Dialog. Then when I enter some keywords in search dialog and press OK, the system will send us a intent containing the search keyword to launch the Searchableactivity interface. So as long as the intent is obtained in the OnCreate method of Searchableactivity, and then the search keyword is obtained, it can be processed.

Searchableactivity.oncreate

    @Override    protectedvoid  onCreate (Bundle savedinstancestate) {         Super . OnCreate (savedinstancestate);         = getintent ();         if (Intent.ACTION_SEARCH.equals (Intent.getaction ())) {            = Intent.getstringextra ( Searchmanager.query); Gets the user input keyword            toast.maketext (this, "The query key is" + Query,toast.length_long). Show (); Pop-up user input keywords, simulate search processing        }    }

After completing the above three steps, a simple search demo is ready. As follows:

Mainactivity to search

searchableactivity Processing Keywords

SOURCE Download HTTP://PAN.BAIDU.COM/S/1O6DTCS6

Basic ways to use Android search controls

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.