Android: how to make keyboard enter button say "Search" and handle its click?
In the layout set your input method options to search.
<EditText
android:imeOptions="actionSearch"
android:inputType="text" />
In the java add the editor action listener.
editText.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;
}
});
In xml
file, put imeOptions="actionSearch"
and inputType="text"
, maxLines="1"
:
<EditText
android:id="@+id/search_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/search"
android:imeOptions="actionSearch"
android:inputType="text"
android:maxLines="1" />
Hide keyboard when user clicks search. Addition to Robby Pond answer
private void performSearch() {
editText.clearFocus();
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(editText.getWindowToken(), 0);
//...perform search
}