Title: can not close search box in actionBar
Tags: android;android-fragments;android-actionbar
Question: I have a search icon in my android actionBar. When in my search fragment it automatically expands and opens the keyboard to search.
When you search it populates a list view, and clicking an item loads a new fragment. I am trying to close the search box back to just the icon when the new fragment loads so the keyboard will disappear but its not working.
My code for the fragment that opens and should close the search box is:
public class BeerPage extends Fragment {
public BeerPage(){}
String beerId = "";
SearchView searchView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.beer_page, container, false);
setHasOptionsMenu(true);
getActivity().setTitle("Beer");
//body code goes here
//get user information
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
String userName = prefs.getString("userName", null);
String userID = prefs.getString("userID", null);
//get bundled data
Bundle args = getArguments();
beerId = args.getString("beerIDSent");
TextView bt = (TextView)rootView.findViewById(R.id.beerBreweryName);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do whatever stuff you wanna do here
//todo: launch new brewery page
Fragment Fragment_one;
FragmentManager man= getFragmentManager();
FragmentTransaction tran = man.beginTransaction();
Fragment_one = new BreweryPage();
final Bundle bundle = new Bundle();
bundle.putString("beerIDSent", beerId);
Fragment_one.setArguments(bundle);
tran.replace(R.id.main, Fragment_one);//tran.
tran.addToBackStack(null);
tran.commit();
}
});
TextView bt2 = (TextView)rootView.findViewById(R.id.beerStyleName);
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do whatever stuff you wanna do here
//todo: launch new brewery page
Fragment Fragment_two;
FragmentManager man= getFragmentManager();
FragmentTransaction tran = man.beginTransaction();
Fragment_two = new StylePage2();
final Bundle bundle = new Bundle();
bundle.putString("beerIDSent", beerId);
Fragment_two.setArguments(bundle);
tran.replace(R.id.main, Fragment_two);//tran.
tran.addToBackStack(null);
tran.commit();
}
});
Button bt3 = (Button)rootView.findViewById(R.id.buttonTasteTag);
bt3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do whatever stuff you wanna do here
//todo: launch new brewery page
Fragment Fragment_three;
FragmentManager man= getFragmentManager();
FragmentTransaction tran = man.beginTransaction();
Fragment_three = new TasteTags2();
final Bundle bundle = new Bundle();
bundle.putString("beerIDSent", beerId);
Fragment_three.setArguments(bundle);
tran.replace(R.id.main, Fragment_three);//tran.
tran.addToBackStack(null);
tran.commit();
}
});
String url = "http://api.brewerydb.com/v2/beer/"+ beerId +"?key=0cdb22f84315834cc807c0176a927060&amp;format=json&amp;withBreweries=y";
new GetBeerDataJSON(getActivity(), beerId).execute(url);
return rootView;
@Override
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateOptionsMenu(menu, inflater);
//inflater.inflate(R.menu.main, menu);
searchView = (SearchView) menu.findItem(R.id.menu_search2).getActionView();
searchView.setIconified(true);
}


Here is another answer: First of all, yay beer!
Second of all, have you tried:
searchView.collapseActionView();
I was working on something to collapse the SearchView and called collapseActionView() in onTabSelected(), which worked for me. So you can try that if you're using tabs.
Cheers!
http://developer.android.com/reference/android/view/MenuItem.html#collapseActionView()
Comment for this answer: actually, it is not the search view that has the method collapseActionView, but the menu item. In the code above, the menu item is the return value of menu.findItem(R.id.menu_search2).

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:02/15/14', 'FavoriteCount': 0.0, 'PostTypeId': 1, 'Tags': '<android><android-fragments><android-actionbar>', 'Title': 'can not close search box in actionBar', 'ViewCount': 354, 'id': 21792556, 'user_AboutMe': None, 'user_AccountId': 1798078.0, 'user_DisplayName': 'Mike', 'user_Reputation': 6611.0, 'user_Views': 484.0, 'user_id': 1637374.0}}