I have an EditText with android:inputType="phone" keybord. There is an "OK" on this keyboard. I like to catch pressing this OK btn. However I don't know the key event. I would like to use code like this:
setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
case KeyEvent.?????????? <- this one please
return true;
default:
break;
}
}
return false;
}
});
I hope the code is different from pressing the "round-arrow"!
I checked all the key events in the docs but could not find anything.
Thanks
The 'OK' button is usually associated with finishing the input, and is different from the others. Use setOnEditorActionListener to catch it:
editText.setOnEditorActionListener( new OnEditorActionListener()
{
public boolean onEditorAction( TextView v, int actionId, KeyEvent event )
{
// Do what you want to do here
}
});
Related
I am trying to respond to pressing the enter button on a standard system on screen soft keybord, in numeric mode. keyboard appears on the screen when myCustomEditText class is given focus.
tried 2 approaches:
overriding onKeyDown():
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
//do sth..
return true;
}
return super.onKeyDown(keyCode, event);
}
setting listener setOnKeyListener:
myCustomEditText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
onRightButtonClicked();
return true;
}
return false;
}
});
(also tried onKeyDown() in myCustomEditText as well, but with no effect)
problem is:
these aproaches above works for all the keys being pressed but for the enter key - the methods are not even being fired at all. (it's not a problem of bad KeyEvent.KEYCODE_...)
my question is:
why these methods are not being fired for enter key, but are for other keys, and how to listen to enter key being pressed properly?
You respond to ENTER differently than normal keys, try something like this:
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
switch (actionId) {
case EditorInfo.IME_ACTION_DONE:
...
return true;
default:
return false;
}
}
});
You can specify which action is performed by ENTER by specifiying android:imeOptions in XML or by calling editText.setImeOptions(...) with one of the constants in EditorInfo. imeOptions also changes how the ENTER key looks! For example if you set imeOptions to actionSearch then the ENTER key will look like a search button.
You can find a list of all possible values for imeOptions along with a description here.
I've found a lot of similar questions but all of them don't work for multilines EditText.
If i set inputType="text" EditText becomes singleline and returns are just not shown. I also need "Enter" button to be "Enter" button (not "Next" or "Done").
content.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN &&
keyCode == KeyEvent.KEYCODE_ENTER)
doSmthSpecial();
return false;
}
});
I don't know why it is not invoking ENTER key event. but you can go for another method which serves you same purpose. use OnEditorActionListener.
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
doSomethingSpecial();
return true;
}
});
this method is invoked when you press ENTER key on EditText.
i have also digged alot of time on internet for this question's solution then,i found this:
et.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
et is you edit text.
this,worked like charm for me.
Ok, so this is silly.
I am using a textbox with the input type textPassword.
On the Galaxy Tab 2 : 10.1 : I can not pick up the "Done" button as in an arrow pointing down then turning left.
I am doing the following check:
tv2.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
performRegister();
return true;
default:
break;
}
}
return false;
}
});
The above code works correctly on 95% of devices that I have tried it is only the Galaxy Tab that is giving me this issue.
I have also tried :
tv2.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//CODE!!
}
});
But neither of these methods get called when I press the button. (Not called at all! It's not that I am not checking for the correct KeyEvent: There is no KeyEvent)
All that this button does is to hide the Keyboard.
Ps. I am using the Galaxy tab with the Samsung Chinese IME keyboard. v2.0.3
Thanks for any help:
Aiden
Try using
TextView.setImeOptions(EditorInfo.IME_ACTION_DONE);
then you should get a callback in your OnEditorActionListener
Hello
I've got a searched EditText and search Button. When I type the searched text, I'd like to use ENTER key on softkeyboard instead of search Button to activate search function.
Thanks for help in advance.
You do it by setting a OnKeyListener on your EditText.
Here is a sample from my own code. I have an EditText named addCourseText, which will call the function addCourseFromTextBox when either the enter key or the d-pad is clicked.
addCourseText = (EditText) findViewById(R.id.clEtAddCourse);
addCourseText.setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
addCourseFromTextBox();
return true;
default:
break;
}
}
return false;
}
});
<EditText
android:id="#+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/search_hint"
android:inputType="text"
android:imeOptions="actionSend" />
You can then listen for presses on the action button by defining a TextView.OnEditorActionListener for the EditText element. In your listener, respond to the appropriate IME action ID defined in the EditorInfo class, such as IME_ACTION_SEND. For example:
EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
sendMessage();
handled = true;
}
return handled;
}
});
Source: https://developer.android.com/training/keyboard-input/style.html
may be you could add a attribute to your EditText like this:
android:imeOptions="actionSearch"
add an attribute to the EditText like
android:imeOptions="actionSearch"
this is the best way to do the function
and the imeOptions also have some other values like "go" 、"next"、"done" etc.
Most updated way to achieve this is:
Add this to your EditText in XML:
android:imeOptions="actionSearch"
Then in your Activity/Fragment:
EditText.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
// Do what you want here
return#setOnEditorActionListener true
}
return#setOnEditorActionListener false
}
We can also use Kotlin lambda
editText.setOnKeyListener { _, keyCode, keyEvent ->
if (keyEvent.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
Log.d("Android view component", "Enter button was pressed")
return#setOnKeyListener true
}
return#setOnKeyListener false
}
To avoid the focus advancing to the next editable field (if you have one) you might want to ignore the key-down events, but handle key-up events. I also prefer to filter first on the keyCode, assuming that it would be marginally more efficient. By the way, remember that returning true means that you have handled the event, so no other listener will. Anyway, here is my version.
ETFind.setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER
|| keyCode == KeyEvent.KEYCODE_ENTER) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
// do nothing yet
} else if (event.getAction() == KeyEvent.ACTION_UP) {
findForward();
} // is there any other option here?...
// Regardless of what we did above,
// we do not want to propagate the Enter key up
// since it was our task to handle it.
return true;
} else {
// it is not an Enter key - let others handle the event
return false;
}
}
});
this is a sample of one of my app how i handle
//searching for the Edit Text in the view
final EditText myEditText =(EditText)view.findViewById(R.id.myEditText);
myEditText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) ||
(keyCode == KeyEvent.KEYCODE_ENTER)) {
//do something
//true because you handle the event
return true;
}
return false;
}
});
how do I catch specific key events from the soft keyboard?
specifically I'm interested in the "Done" key.
I am not quite sure which kind of listener was used in the accepted answer.
I used the OnKeyListener attached to an EditText and it wasn't able to catch next nor done.
However, using OnEditorActionListener worked and it also allowed me to differentiate between them by comparing the action value with defined constants EditorInfo.IME_ACTION_NEXT and EditorInfo.IME_ACTION_DONE.
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((actionId & EditorInfo.IME_MASK_ACTION) != 0) {
doSomething();
return true;
}
else {
return false;
}
}
});
#Swato's answer wasn't complete for me (and doesn't compile!) so I'm showing how to do the comparison against the DONE and NEXT actions.
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
int result = actionId & EditorInfo.IME_MASK_ACTION;
switch(result) {
case EditorInfo.IME_ACTION_DONE:
// done stuff
break;
case EditorInfo.IME_ACTION_NEXT:
// next stuff
break;
}
}
});
Also I want to point out that for JellyBean and higher OnEditorActionListener is needed to listen for 'enter' or 'next' and you cannot use OnKeyListener. From the docs:
As soft input methods can use multiple and inventive ways of inputting text, there is no guarantee that any key press on a soft keyboard will generate a key event: this is left to the IME's discretion, and in fact sending such events is discouraged. You should never rely on receiving KeyEvents for any key on a soft input method.
Reference: http://developer.android.com/reference/android/view/KeyEvent.html
Note: This answer is old and no longer works. See the answers below.
You catch the KeyEvent and then check its keycode. FLAG_EDITOR_ACTION is used to identify enter keys that are coming from an IME whose enter key has been auto-labelled "next" or "done"
if (event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION)
//your code here
Find the docs here.
just do like this :
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_DONE)
{
//Do Something
}
return false;
}
});
etSearchFriends = (EditText) findViewById(R.id.etSearchConn);
etSearchFriends.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
Toast.makeText(ACTIVITY_NAME.this, etSearchFriends.getText(),Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
To catch a “Done” key press from the soft keyboard override Activity's onKeyUp method.
Setting a OnKeyListener listener for a view won't work because key presses in software input methods will generally not trigger the methods of this listener, this callback is invoked when a hardware key is pressed in the view.
// Called when a key was released and not handled by any of the views inside of the activity.
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
// code here
break;
default:
return super.onKeyUp(keyCode, event);
}
return true;
}
Note : inputtype mention in your edittext.
<EditText android:id="#+id/select_category"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" >
edittext.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((actionId & EditorInfo.IME_MASK_ACTION) == EditorInfo.IME_ACTION_DONE) {
//do something here.
return true;
}
return false;
}
});
you can override done key event by this method:
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// do your stuff here
}
return false;
}
});
KOTLIN version:
<EditText android:id="#+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
Do not forget to set android:inputType.
// Get reference to EditText.
val editText = findViewById<EditText>(R.id.edit_text)
editText.setOnEditorActionListener { _, actionId: Int, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
// Do your logic here.
true
} else {
false
}
}
I have EditText that searches names, and it automatically shows results below in ListView. SoftInput keyboard only showed "next" button and enter sign - which didn't do anything. I wanted only Done button (no next or enter sign) and also I wanted it when it was pressed, it should close keyboard because user should see results below it.
Solution that I found /by Mr Cyril Mottier on his blog/ was very simple and it worked without any additional code:
in xml where EditText is located, this should be written:
android:imeOptions="actionDone"
so hidding keyboard with Done button, EditText should look like this:
<EditText
android:id="#+id/editText1central"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/imageView1"
android:layout_toLeftOf="#+id/imageView2tie"
android:ems="10"
android:imeOptions="actionDone"
android:hint="#string/trazi"
android:inputType="textPersonName" />
IME_MASK_ACTION is 255, while the received actionId is 6, and my compiler does not accept
if (actionId & EditorInfo.IME_MASK_ACTION)
which is an int. What is the use of &-ing 255 anyway? So the test simply can be
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE)
...