I simply want to catch the event when the user press enter on an editText.
I did not get the Toast message, not the \"Enter pressed\" and not the \"Some key pressed!
myEditText.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
Toast.makeText(getApplicationContext(), "some key pressed", Toast.LENGTH_LONG)
.show();
return false;
}
});
This code displays me some key pressed when pressed enter key after typing something in my edittext. But I don't know what is problem in your code.
I don't know why Atul Chatuverdi's answer was downvoted, -- after lots of browsing, this one finally did "a wonder" to me.
I confirm that ActionDone may not work. It works on my Samsung but for some reason doesn't work on Fly S. The event is just not fired. Both are Android 7.1. I wonder, if it is a bug of Google's keyboard...
The code that I used to make it finally work on all devices is
<EditText
android:singleLine="true"
android:inputType="textUri"
android:maxLines="1"
android:imeOptions="actionGo"
...
android:id="@+id/et_path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
(You can use text instead of textUri, depending on your needs).
In my fragment:
editText = view.findViewById(R.id.et_path);
...
editText.setOnEditorActionListener(new onGo());
and the nested class to process the action.
private class onGo implements TextView.OnEditorActionListener {
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if (i == EditorInfo.IME_ACTION_GO) {
// To do stuff
return true;
}
return false;
}
}
As for if (event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER))
in a multiline text, I have the same problem. It works with Samsung keyboard, Hacker's keyboard, but not Google keyboard.
Advice to use a textwatcher is a no-go for various reasons (like pasting text with enter signs instead of pressing the enter button and others).
Please try to set the EditText
in single line mode.
editText.setSingleLine();
or in your xml layout file:
android:singleLine="true"
UPDATE
is deprecated. From now on, use android:singleLine="true"
android:maxLines="1"
with android:inputType="text"
for achieving this behaviour
OR
editText.setMaxLines(1);
editText.setInputType(InputType.TYPE_CLASS_TEXT);
for setting programmatically.
The problem for me was that I had set in XML android:inputType="textMultiline"
. When I removed textMultiline
option, the EditorListener
started working.
android:inputType="text"
and android:imeOptions="actionGo"
do the wonder.
use android:imeOptions="actionGo"
in xml. actionDone
no longer responds to onEditorAction
.