20

I change android soft keyboard to show search icon rather than Enter icon. My question is: how i can detect that user click on that search button?

2 Answers 2

48

In the edittext you might have used your input method options to search.

<EditText
      android:imeOptions="actionSearch"
      android:inputType="text"/>

Now use the Editor listener on the EditText to handle the search event as shown below:

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
           // Your piece of code on keyboard search click 
            return true;
        }
        return false;
    }
});
4
  • my code doesn't run with this method!! it doesn't work for me
    – Fcoder
    Mar 30, 2012 at 18:54
  • 2
    "it doesn't work" have never helped anyone. Being specific is the most awesome thing !
    – f2lollpll
    Mar 30, 2012 at 21:28
  • 1
    Note you seem to also need "android:inputType="text"" on modern (4+) android.
    – Fattie
    Jun 10, 2014 at 15:52
  • Great! Save lots of time. Was facing same issue.
    – Smeet
    Mar 5, 2015 at 12:59
4

You can use OnQuerySubmit listener to get keyboard Search button click event.

Here's how,

searchView.setOnQueryTextListener(queryTextListener);

SearchView.OnQueryTextListener queryTextListener
        = new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String s) {
        showLog("searchClickListener onQueryTextSubmit");
        getPhotos(searchView.getQuery().toString());
        return true;
    }

    @Override
    public boolean onQueryTextChange(String s) {
        return false;
    }
};

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.