In my application I have an actionbar activity that holds multiple fragments. In the activity when I do search the results are shown in a new fragment. I have tried hiding the keyboard after I click the submit button without any luck. Here is my code
public class SymptomNavigator extends ActionBarActivity implements
MainBodyArea.Communicator, SearchResult.communicator, OnClickListener {
SearchView searchView;
FragmentManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.symptom_navigator);
Button generateButton = (Button) findViewById(R.id.btn_Generate);
generateButton.setOnClickListener(this);
Button symptomListButton = (Button) findViewById(R.id.btn_ViewList);
symptomListButton.setOnClickListener(this);
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null) {
return;
}
MainBodyArea firstFragment = new MainBodyArea();
firstFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
}
manager = getSupportFragmentManager();
mainFrag = (MainBodyArea) manager
.findFragmentById(R.id.mainBodyFragment);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
searchView = (SearchView) MenuItemCompat
.getActionView(searchItem);
final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
query.trim();
searchView.clearFocus();
if (FormValidator.isValidEntry(query)) {
respondToSearch(query);
}
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
return true;
}
@Override
public void respondToSearch(String value) {
SearchResult newFragment = new SearchResult();
Bundle args = new Bundle();
args.putString(SearchResult.ARG_POSITION, value);
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack("search");
getSupportFragmentManager().popBackStack("search", FragmentManager.POP_BACK_STACK_INCLUSIVE);
transaction.commit();
}
}
As you can see in the above code I have called clearFocus on the search view inside the onqquery textlistener. It hides the keyboard and again shows it after the new fragment transaction is done. How do I make it work?
Note: I have also tried the following approach without any luck
try {
InputMethodManager input = (InputMethodManager) getActivity()
.getSystemService(Activity.INPUT_METHOD_SERVICE);
input.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
}catch(Exception e) {
e.printStackTrace();
}
After hours of search I have managed to fix it with a single line of code. As I said the issue was the action bar is getting focused after results are generated.
All I had to do was to put this
searchView.setFocusable(false);
before calling the results fragment. Hope this helps someone!