Directory (?) [-]
- Search bars in the Actionbar
- Define search view on menu item
- To configure the searchable
- Associating search view with searchable activity in activity
- Code for searchable activity
Search bars in the Actionbar
We can also embed the search bar in action Bar. In the small example, we embed a search box widget (called Search view) in Action Bar. When we enter a search, we can open it in the specified activity (called searchable activitiy), this example does not do a real search, but displays the content to be searched in the specified activity.
Define search view on menu item
<?xml version= "1.0" encoding= "Utf-8"?>
<menu xmlns:android= "Http://schemas.android.com/apk/res/android" >
<item android:id= "@+id/menu_search"
Android:title= "Search"
android:showasaction= "Ifroom"
android:actionviewclass= "Android.widget.SearchView" />
... Other Item content ...
The android:showasaction indicates that this is the menu item on the Action Bar, if we set the property to: android:showasaction= "Collapseactionview|ifroom", as shown in the figure on the right. Collapseactionview allows you to extend the search view to the entire action bar. For mobile phones, extending the length of the input box has a better user experience. In this case, when not in use, the UI is the same as the normal Action bar menu item, so we need to set up android:icon= "@drawable/ic_search" to display the icon.
by android:actionviewclass= "Android.widget.SearchView" We will embed the search view widget here, corresponding to the Searchview class.
To configure the searchable
Search view can be viewed as a widget that is displayed for a searchable activity in other activity's action bar. With this widget, you can invoke searchable activity. The configuration of Search view can be the same as the Res/xml/searchable.xml file. For more configuration properties, see: Http://developer.android.com/guide/topics/search/searchable-config.html. This example reads as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<searchable xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:label= "@string/search_result" Wei: Must contain Android:label, usually the same as the app or activity label, but not as well as effect
android:hint= "@string/search_hint" Wei: Hint is generally recommended as an input box hint
/>
Search view is the searchable activity at the entrance of other activity, its configuration is set by searchable activity, in Androidmanifest.xml, the relevant fragment is as follows. Searchable activity must be action_search.
<activity android:name= ". Searchresultactvity "android:label=" @string/search_result ">
<intent-filter>
<action android:name= "Android.intent.action.SEARCH"/>
</intent-filter>
<meta-data android:name= "android.app.searchable" android:resource= "@xml/searchable"/>
</activity>
Associating search view with searchable activity in activity
There is no difference between adding a search view and adding an action bar to the activity, or adding the Options menu,
public boolean Oncreateoptionsmenu (Menu menu) {
Add a menu containing search view
Menuinflater inflater = Getmenuinflater ();
Inflater.inflate (menuId, menu);
//Get Searchview object
Searchview Searchview = (searchview) menu.finditem (r.id.menu_search). Getactionview ();
if (Searchview = = null) {
LOG.E ("Searchview", "Fail to get Search View.");
return true;
}
Searchview.seticonifiedbydefault (TRUE); The default value is true, which may not be set specifically, false and true as follows, and the input box for true is larger
//Get Search Service Manager
Searchmanager Searchmanager = (searchmanager) getsystemservice (Context.search_service);
Searchable activity's component name, which can be recalled by intent
ComponentName cn = New ComponentName (this,searchresultactvity.class);
With the search manager, obtaining relevant search information from searchable activity is the searchable XML setting. If NULL is returned, indicates that the activity does not exist or is not searchable
Searchableinfo info = Searchmanager.getsearchableinfo (CN);
if (info = = null) {
LOG.E ("Searchableinfo", Fail to get search info. ");
}
//Link The search information for searchable activity with the searches view
Searchview.setsearchableinfo (info);
return true;
}
If we want to get a system global search, we can get it by componentname cn = Searchmanager.getglobalsearchactivity (). You can view the system's active global Search by list<searchableinfo> list = Searchmanager.getsearchablesinglobalsearch ().
Code for searchable activity
public class Searchresultactvity extends activity{
@Override
protected void OnCreate (Bundle savedinstancestate) {
... ...
Dosearchquery (getintent ());
}
@Override
protected void Onnewintent (Intent Intent){//activity re-pinned
Super.onnewintent (Intent);
Dosearchquery (Intent);
}
//The call to searchable activity is still a standard intent, we can get information from intent, that is, what to search for
private void Dosearchquery (Intent Intent) {
if (intent = = NULL)
Return
String queryaction = Intent.getaction ();
if (Intent.ACTION_SEARCH.equals (Intent.getaction ())) {//If it is called through Action_search, that is, if it is called through a search
String queryString = Intent.getstringextra (searchmanager.query); Get Search Content
... ...
}
}
... ...
}
For references see: http://developer.android.com/training/search/setup.html
The example code covered in this blog post can be downloaded in Pro Android Learning: Actionbar Small example.
RELATED Links: My Android development related articles
"Turn" Pro Android Learning Note (50): ActionBar (3): Search bar