setImeActionLabel take two parameters, and the second int parameter should be one of those that are in the EditorInfo class. For instance:
EditorInfo.IME_ACTION_GO EditorInfo.IME_ACTION_DONE EditorInfo.IME_ACTION_NEXT ....
You cannot send any other integer there, for example KeyEvent.KEYCODE_ENTER
And you must set the XML as a parameter imeOptions , and singleLine , to make it work. Example:
<EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:imeOptions="actionGo" android:singleLine="true"/>
Here is the code I used and it works:
XML layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/editText2" android:layout_width="match_parent" android:layout_height="wrap_content" android:singleLine="true"/> </LinearLayout>
And the base Activity code:
mEditText2.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { boolean handled = false; if (actionId == EditorInfo.IME_ACTION_GO) { Toast.makeText(MainActivity.this, "You entered " + v.getText().toString(), Toast.LENGTH_LONG).show(); handled = true; } return handled; } }); mEditText2.setImeActionLabel("Search Model", EditorInfo.IME_ACTION_GO);
source share