This example describes the use of Android Actionbar search functionality. Share to everyone for your reference, specific as follows:
Points to note when using Actionbar Searchview:
The first thing to do is to spit out the official guide documentation for Android, which is unclear about usage and may be why it hasn't been updated.
Originally according to the document, hint not come out, also can't jump to search results activity.
StackOverflow also someone raised the question, the answer is very clear-reference links.
Correct usage
1. Add Meta-data to the activity in androidmanifest.xml for the provision of Searchview
<activity
android:name= ". Navigation. Navigationactivity "
android:label=" @string/title_activity_navigation ">
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= " Android.intent.category.DEFAULT "/>
</intent-filter>
<meta-data
android:name=" Android.app.default_searchable "
android:value=". Search. Searchresultactivity "/>
</activity>
2. Add Meta-data to Searchableinfo in activity that provides search results
<activity
android:name= ". Search. Searchresultactivity "
android:label=" @string/title_activity_search_result "
android:launchmode=" Singletop ">
<intent-filter>
<action android:name=" Android.intent.action.SEARCH "/>
</intent-filter>
<!--This metadata entry provides further configuration details for searches-->
<!--that are handled by this activity.-->
<meta-data
android:name= "android.app.searchable"
Android:resource= "@xml/searchable"/>
</activity>
3. The android:hint in the @xml/searchable file can only use strings defined in String.xml, not hard-coded
<?xml version= "1.0" encoding= "Utf-8"?> <searchable xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:label=" @string/app_name "
android:hint=" @string/search_hint ">
</ Searchable>
4. When the menu is initialized, get Searchableinfo
Searchmanager Searchmanager = (searchmanager) getsystemservice (context.search_service);
Searchview Searchview = (searchview) menu.finditem (r.id.action_search). Getactionview ();
Searchview.setsearchableinfo (Searchmanager.getsearchableinfo (Getcomponentname ()));
Pay attention.
Searchmanager through ComponentName to find Searchableinfo, the corresponding component must meet certain conditions:
1. Intent-filter contains
<action android:name= "Android.intent.action.SEARCH"/>
2. Meta-data
<meta-data
android:name= "android.app.searchable"
android:resource= "@xml/searchable"/>
Another way
Since Searchmanager is componentname to get searchableinfo, of course, you can get componentname directly from the activity that provides the search results.
Searchmanager Searchmanager = (searchmanager) getsystemservice (context.search_service);
Searchview Searchview = (searchview) menu.finditem (r.id.action_search). Getactionview ();
ComponentName cn = New ComponentName ("Com.liangfeizc.catykanji", " Com.liangfeizc.catykanji.search.SearchResultActivity ");
Searchview.setsearchableinfo (Searchmanager.getsearchableinfo (CN));
Tips
The first parameter of the ComponentName constructor pkg is the package of the application, not the package where the target class resides.
The parameter componentname (string pkg, string cls) is application package not the package where the
More interested readers of Android-related content can view the site's topics: "The Overview of Android View View Tips", "Android operation XML Data Skills Summary", "Android programming activity Operation Skills Summary", " Android Resource Operation tips Summary, "Android file Operation Tips", "Android operation SQLite Database Skills Summary", "Android operation JSON format Data Skills summary", "Android Database Operating skills summary", " Android programming development of the SD card operation method Summary, "Android Development introduction and Advanced Course" and "Android Control usage Summary"
I hope this article will help you with your Android programming.