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();
}
}