Android actionbar search box

Source: Internet
Author: User

Is to implement such a search box on the top.

1. The search box is an item on the menu on the actionbar. It is called SearchView. We can define it in the menu option first:

Bmap_menu.xml:

 
       
  
 

Here, the collapseActionView of showAsAction indicates that searchView can be extended to the entire actionbar.

2. Configure searchble:

Search View can be seen as a quick entry ID of seachble activity in acionbar. Then we can configure the relevant information by configuring searchble:

In the res/xml folder (if there is no xml folder, create a new file) and create the file searchable. xml.

 
 

3. Configure AndroidManifest. xml

Since searchview is an entry identifier, it takes a long time to configure filtering in the AndroidManifest. xml file:

            
                              
                      
  

4. Loading searchble configuration information in searchView:

Public boolean onCreateOptionsMenu (Menu menu) {// Add MenuInflater inflater = getMenuInflater (); inflater. inflate (menuId, menu); // obtain the 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. fa The lse and true values are as follows. The true input box is larger // obtain the search service manager SearchManager = (searchManager) getSystemService (Context. SEARCH_SERVICE); // searchable activity's component name. The system can use intent to evoke ComponentName cn = new ComponentName (this, SearchResultActvity. class); // use the search manager to obtain the search information from the searchable activity, that is, the xml settings of searchable. If null is returned, the activity does not exist or is not searchable SearchableInfo = searchManager. getSearchableInfo (cn); if (info = null) {Log. e ("SearchableInfo", Fail to get search info. ");} // associate the searchable activity search information with the search view. setSearchableInfo (info); return true ;}
5. Response to the search activity:

Public class BmapActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState ){... ... DoSearchQuery (getIntent ();} @ Override protected void onNewIntent (Intent intent) {// reset the activity to super. onNewIntent (intent); doSearchQuery (intent) ;}// the call to searchable activity is still a standard intent. We can obtain information from the intent, private void doSearchQuery (Intent intent) {if (intent = null) return; String queryAction = intent. getAction (); if (Intent. ACTION_SEARCH.equals (intent. getAction () {// if it is through ACTION_SEARCH To call, that is, if you call String queryString = intent. getStringExtra (SearchManager. QUERY) by searching; // obtain the search content... ... }}... ... }

In addition, there are several requirements:

1. it seems easy to change the color of the horizontal line under the default search box. In fact, it is very troublesome to set the background image directly. I think the methods of people who read the overflow forum on the Internet are not ideal. Some people have proposed a reflection solution. I also adopt the reflection method:

try{Class
  argClass=searchView.getClass();               Field ownField = argClass.getDeclaredField("mSearchPlate");              //setAccessible              ownField.setAccessible(true);              View mView = (View) ownField.get(searchView);              mView.setBackground(getResources().getDrawable(R.drawable.test));  }catch(Exception e){e.printStackTrace();}
However, there is a problem. If your searchView has a submit, it is only the horizontal line under the search box that changes color, and the color under the submit is still blue. Currently, I cannot find the name of the submit member variable. I don't know if you have any good solutions.

2. Expand searchView by default:

Many people have this requirement. You can expand the searchView by default, and add some default text to it (not hint ).

Use this extension:

MenuItem searchItem = menu.findItem(R.id.menu_search);searchItem.expandActionView();

Set the text as follows:

searchView.setQuery("ok", false);


If this is the case, the virtual keyboard will also be opened, affecting the user experience. The method to disable the virtual keyboard is to clear the focus of the searView:

searchView.clearFocus();

Refer:

Google Development Documentation: http://developer.android.com/training/search/setup.html

Kai Feng's blog http://blog.csdn.net/flowingflying/article/details/14163401

Related Article

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.