Android: Edit Text Go Button

AndroidEditIme

Android Problem Overview


I have an Edit Text that is defined as follows.

<EditText  
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:maxLines="1"
android:inputType="text" 
android:hint="@string/field_text"
android:id="@+id/field"
/>

I want to set a custom command so that when somebody clicks on the Done/Go button on the onscreen keyboard a button is clicked or just run the methods that are run by the button. I think this has something to do with ime options but I havent been able to figure out how they work. Thanks in advance for any help!

Android Solutions


Solution 1 - Android

You want a combination of android:imeOptions and setOnEditorActionListener

<EditText android:id="@+id/some_edittext"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:imeOptions="actionSend">
</EditText>


some_edittext.setOnEditorActionListener(new OnEditorActionListener() {
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEND) {
            some_button.performClick();
            return true;
        }
        return false;
    }
});

Obviously you should change actionSend to the action you want, and update IME_ACTION_SEND correspondingly.

Solution 2 - Android

Take a look at the setImeActionLabel method (or imeActionLabel and imeActionId attributes) and setOnEditorActionListener to set a listener to respond to the events.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCorey AlexanderView Question on Stackoverflow
Solution 1 - AndroidNiall SheridanView Answer on Stackoverflow
Solution 2 - AndroidadampView Answer on Stackoverflow