Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

actionbar search will not close after search

I have an actionbar with a search icon. When the search icon is clicked, it expands to a search bar in which the user can type in a search.

The problem is when the user enters the search I would like the search bar to collapse back to an icon but I can not get it to happen for the life of me.

My actionbar menu looks like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity" >

    <item android:id="@+id/menu_search2"
        android:actionViewClass="android.widget.SearchView"
        android:title="Search"
        android:icon="@android:drawable/ic_menu_search"
        android:showAsAction="always|collapseActionView|"
        android:onClick="goToSearch"
        />

    <item android:id="@+id/action_scan"
        android:icon="@drawable/barcode"
        android:onClick="scanBarcode"
        android:showAsAction="ifRoom|collapseActionView"
        />

</menu>

My search activity looks like this:

public class Search extends Fragment implements SearchView.OnQueryTextListener, ReadJSONResult.OnArticleSelectedListener {

    private ListView lv;
    View v;
    SearchView searchView;
    private SearchView mSearchView;
    private MenuItem mSearchMenuItem;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        //set layout here
        v = inflater.inflate(R.layout.activity_search, container, false);
        setHasOptionsMenu(true);
        getActivity().setTitle("Search");


        //get user information
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
        String userName = prefs.getString("userName", null);
        String userID = prefs.getString("userID", null);


        return v;


}

    @Override
    public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
        // Inflate the menu; this adds items to the action bar if it is present.

        searchView = (SearchView) menu.findItem(R.id.menu_search2).getActionView();
        searchView.setOnQueryTextListener(this);
        searchView.setIconified(false);
    }


    public boolean onQueryTextSubmit (String query) {

        //toast query
        //make json variables to fill
        searchView.setIconified(true);
        searchView.clearFocus();

        // url to make request
        String url = "myURL";



        try {
            query = URLEncoder.encode(query, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        String jsonUrl = url + query;


        //todo: get json
        ReadJSONResult task = new ReadJSONResult(getActivity());
        task.setOnArticleSelectedListener(this);
        task.execute(jsonUrl);

        return false;
    }





    @Override
    public boolean onQueryTextChange(String newText) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onArticleSelected(String b, String brewery){
        searchView.setIconified(true);
        searchView.clearFocus();
        searchView.postInvalidate();
        //code to execute on click
        Fragment Fragment_one;
        FragmentManager man= getFragmentManager();
        FragmentTransaction tran = man.beginTransaction();

        //adds beer data to shared prefs for beer tabs
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("beerID",b);
        editor.putString("breweryID",brewery);
        editor.commit();
        Fragment_one = new BeerTabs();

        tran.replace(R.id.main, Fragment_one);//tran.
        tran.addToBackStack(null);
        tran.commit();

    }



}
like image 559
Mike Avatar asked Apr 14 '14 23:04

Mike


2 Answers

One simpler way, that works even if the SearchView widget is not on the actionbar is the following code:

searchView.setQuery("", false);
searchView.setIconified(true);

And that makes the SearchView collapse after a search is made.

like image 86
Nick Avatar answered Nov 19 '22 15:11

Nick


After some searches, I find the solution. The onActionViewCollapsed works but you had an unexpected behaviour (icon jumps to left, up indicator still here...) - this (hard) solution was suggested in my first and previous answer, and I was persuaded by use of collapseActionView method. However, SearchView.collapseActionView() was not working because according to the Documentation:

Collapse the action view associated with this menu item.

It's related to the MenuItem and not to the SearchView widget. That's why you had this error when you used this method:

The method collapseActionView() is undefined for the type SearchView

Then, the solution is to create a Menu variable as follows:

 // At the top of your class
 private Menu mMenu;

 // onCreateOptionsMenu method
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
      this.mMenu = menu; // init the variable
      // other stuff..
      return true;
 }

 // call the collapseActionView method
 public boolean onQueryTextSubmit(String query) {
      searchView.setIconified(true);
      searchView.clearFocus();
      // call your request, do some stuff..

      // collapse the action view
      if (mMenu != null) { 
          (mMenu.findItem(R.id.menu_search2)).collapseActionView();
      }
      return false;
 }

Or another might be to avoid the implement SearchView.OnQueryTextListener and to do it inside onCreateOptionsMenu as follows:

@Override
// make your Menu as 'final' variable
public void onCreateOptionsMenu (final Menu menu, MenuInflater inflater) {
    searchView = (SearchView) menu.findItem(R.id.menu_search2).getActionView();
    // call the query listener directly on the SearchView
    searchView.setOnQueryTextListener(new OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            searchView.setIconified(true);
            searchView.clearFocus();

            // call the request here

            // call collapse action view on 'MenuItem'
            (menu.findItem(R.id.menu_search2)).collapseActionView();

            return false;
        }
        @Override
        public boolean onQueryTextChange(String newText) { return false; }
    });
    searchView.setIconified(false);
}  

This will resolve the issue for sure. Happy coding!

like image 34
Blo Avatar answered Nov 19 '22 15:11

Blo