Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to make keyboard enter button say "Search" and handle its click?

I can't figure this out. Some apps have a EditText (textbox) which, when you touch it and it brings up the on-screen keyboard, the keyboard has a "Search" button instead of an enter key.

I want to implement this. How can I implement that Search button and detect press of the Search button?

Edit: found how to implement the Search button; in XML, android:imeOptions="actionSearch" or in Java, EditTextSample.setImeOptions(EditorInfo.IME_ACTION_SEARCH);. But how do I handle the user pressing that Search button? Does it have something to do with android:imeActionId?

like image 534
Ricket Avatar asked Jul 08 '10 15:07

Ricket


People also ask

How do I change the Enter button on my Android keyboard?

Go to your Phone Settings. Find and tap Languages and input. Tap on current keyboard under Keyboard & input methods. Tap on choose keyboards.

How do I press Enter on Android?

Show activity on this post. You can use newline in Android Jelly Bean while texting too. While typing hold shift key the smiley icon will change to newline icon then move your finger to that newline button, it gives me newline in my text message.

How do I dismiss my keyboard on Android?

To dismiss the keyboard, call clearFocus() on the respective element when the button is clicked.


2 Answers

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;     } }); 
like image 185
Robby Pond Avatar answered Oct 14 '22 02:10

Robby Pond


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 } 
like image 25
kaMChy Avatar answered Oct 14 '22 01:10

kaMChy