Reputation: 9924
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!" either.
What m i doing wrong?
myEditText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Toast.makeText(getApplicationContext(), "Some key pressed!", Toast.LENGTH_LONG).show();
if (event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
Toast.makeText(getApplicationContext(), "Enter pressed", Toast.LENGTH_LONG).show();
return true;
}
return false;
}
});
E D I T :
Well it is working on Android 2.3.3 and not working on 4.1.2 Any ideas how can i make this work on any android device?
Upvotes: 14
Views: 16885
Reputation: 15525
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.
Upvotes: 2
Reputation: 511
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).
Upvotes: 1
Reputation: 61
android:inputType="text"
and android:imeOptions="actionGo"
do the wonder.
Upvotes: 3
Reputation: 11606
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.
Upvotes: 29
Reputation: 63
The problem for me was that I had set in XML android:inputType="textMultiline"
. When I removed textMultiline
option, the EditorListener
started working.
Upvotes: 2
Reputation: 783
use android:imeOptions="actionGo"
in xml. actionDone
no longer responds to onEditorAction
.
Upvotes: 3