Thursday, May 17, 2012

SerarchView For Android 3.1


SearchView


A widget that provides a user interface for the user to enter a search query and submit a request to a search provider. Shows a list of query suggestions or results, if available, and allows the user to pick a suggestion or result to launch into.

Example

Step - 1:

- Create /xml directory in /res.
- Create searchview.xml in /res/xml.
- Add <searchable /> tag in searchview.xml.
     <searchable
         xmlns:android="http://schemas.android.com/apk/res/android"
         android:label="SearchView">
     </searchable>
- Register SearchableActivity in AndroidManifest.xml file under the <application /> tag.
    <activity android:name=".SearchableActivity">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>

        <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchview" />
        </activity>
Step - 2:
- Add SearchView in /res/layout/main.xml
    <SearchView
        android:id="@+id/searchView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </SearchView>
Step - 3:
- Implements the listener on SearchViewDemoActivity activity.
     public class SearchViewDemoActivity extends Activity implements SearchView.OnQueryTextListener, SearchView.OnCloseListener {
- Add the below code to search applications.
    /*** GET THE SERVICE FOR SEARCHING THE APPLICATIONS ***/                                                  
    final SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);             
    if(searchManager != null) {                                                                               

        /*** GET THE APPLICATIONS WITH GLOBAL SEARCH ***/                                                     
        List<SearchableInfo> searchableInfos = searchManager.getSearchablesInGlobalSearch();                  

        SearchableInfo info = searchManager.getSearchableInfo(getComponentName());                            
        for(SearchableInfo info2 : searchableInfos) {                                                         
            if(info2.getSuggestAuthority() != null && info2.getSuggestAuthority().startsWith("applications")) {
                info = info2;                                                                                 
            }                                                                                                 
        }                                                                                                      
        searchView.setSearchableInfo(info);                                                                   
    }                                                                                                         
Step - 4:
- Run application.
Find the attached zip file for complete source code.
I would be glad to receive your suggestions regarding it.
Thanks.





No comments:

Post a Comment