0

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();
        }
Much Overflow
  • 3,142
  • 1
  • 23
  • 40
  • Try passing the window token as an argument from the view where you're performing the search. Usually you need to use the window token for the view that opened the keyboard. Use a callback from the fragment to your activity, when search is complete, if the content is being entered in an EditText, use editText.getWindowToken(). – Rarw May 21 '14 at 13:28

2 Answers2

2

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!

Much Overflow
  • 3,142
  • 1
  • 23
  • 40
1

Try this to hide the soft keyboard

getWindow().setSoftInputMode(
              WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

To hide always use the following,

getWindow().setSoftInputMode(
              WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

by using this the keyboard is shown only when selecting a textfield or something like that

Ciril
  • 420
  • 3
  • 9
  • Tried, still the same. After I do the search and the result fragment is loaded, the focus automatically goes to the actionbar search even after I tried searchView.clearFocus() – Much Overflow May 21 '14 at 13:33
  • Remove the auto focus, if not necessary. – Ciril May 21 '14 at 13:37
  • Tried it now without luck. Maybe the appcompat search widget receives focus by default even if autofocus is disabled. – Much Overflow May 21 '14 at 13:50