Jaehak
Jaehak

Reputation: 21

Android - How to hide keyboard when search button of the keyboard is pressed?

How to hide keyboard when search button of the keyboard is pressed?

When the user finishes inputing in edittext and press 'search button' fo the keyboard, the keyboard should be hidden..

Upvotes: 1

Views: 3804

Answers (1)

Rohit5k2
Rohit5k2

Reputation: 18112

Set imeOptions to "actionSearch" in your xlm for edit text

Initialize listeners for your EditText

searchEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            performSearch();
            return true;
        }

        return false;
    }
});

Hide keyboard when user clicks search.

private void performSearch() {
    searchEditText.clearFocus();
    InputMethodManager in = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    in.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);
    ... perform search ...
}

Upvotes: 7

Related Questions