How to build Gmail like search box in the action bar?

后端 未结 7 1697
一整个雨季
一整个雨季 2020-12-07 23:14

I am currently using SearchView widget inside ActionBarcompat to filter a list while searching.

When the user starts entering text the Li

7条回答
  •  余生分开走
    2020-12-07 23:57

    I've successfully used Michaels reply (https://stackoverflow.com/a/18894726/2408033) but I didn't like how manual it was, inflating the views and adding it to the actionbar, toggling its state etc.

    I modified it to to use an ActionBar ActionView instead of adding the view manually to the actionbar/toolbar.

    I find this works a lot better since I don't need to manage the open/close state and hiding of views as he did in his example in the toggleSearch method in the added link. It also works perfectly with the back button.

    In my menu.xml

     
    

    In my onCreateOptionsMenu

     View actionView = menu.findItem(R.id.global_search).getActionView();
     searchTextView = (ClearableAutoCompleteTextView) actionView.findViewById(R.id.search_box);
     searchTextView.setAdapter(searchAdapter);
    

    You can find a fully working version of the implementation in my project. Note there are two search views as I was using the actual SearchView to filter a listView.

    https://github.com/babramovitch/ShambaTimes/blob/master/app/src/main/java/com/shambatimes/schedule/MainActivity.java

提交回复
热议问题