Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listen to ENTER key in Android

Here is my code:

public class CaptureENTER extends Activity implements OnKeyListener{

/* on create and other stuff in here*/

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                (keyCode == KeyEvent.KEYCODE_ENTER)) {
           Toast.makeText(LiVoiceActivity.this,
                                     "YOU CLICKED ENTER KEY",
                                     Toast.LENGTH_LONG).show();

        }       
        return false;
    }

I don't know what is going on, but when I press the ENTER key in my keyboard (I am using the Android emulator), the event is not activated.

What am I missing?

like image 772
Carlos Pereira Avatar asked Jun 28 '12 03:06

Carlos Pereira


People also ask

How do I find my enter key on Android?

myTextView. setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { boolean handled = false; if (event. getAction() == KeyEvent. KEYCODE_ENTER) { // Handle pressing "Enter" key here handled = true; } return handled; } });

How do you press Enter on Android keyboard?

You can use newline in Android Jelly Bean while texting too. While typing hold shift key the smiley icon will change to newline icon then move your finger to that newline button, it gives me newline in my text message.

How do I close the keyboard on Android?

HIDE_IMPLICIT_ONLY, 0); Here pass HIDE_IMPLICIT_ONLY at the position of showFlag and 0 at the position of hiddenFlag . It will forcefully close soft Keyboard.


2 Answers

Try this:

public class CaptureENTER extends Activity implements OnKeyListener{ 

/* on create and other stuff in here*/ 

    @Override 
    public boolean onKey(View v, int keyCode, KeyEvent event) { 
        if ((event.getAction() == KeyEvent.ACTION_DOWN) && 
                (keyCode == KeyEvent.KEYCODE_ENTER)) { 
           Toast.makeText(LiVoiceActivity.this, 
                                     "YOU CLICKED ENTER KEY", 
                                     Toast.LENGTH_LONG).show(); 
          return true;

        }        
        return false; 
    }

EDIT: David is CORRECT!!

Returning true is not the issue.

You're failing because you must set the listener to a View, not just the Activity.

like image 37
Yaqub Ahmad Avatar answered Nov 12 '22 01:11

Yaqub Ahmad


Returning true is not the issue.

You're failing because you must set the listener to a View, not just the Activity.

edited for clarification:

The return value of the listener is not meant to be understood as a signal that the event will or will not be called. And it couldn't anyway, since the return clause is only called after your Toast is shown.

It's a signal to the system that further action is needed (return false) or that the method handled the event fully and properly (return true). That's why the documentation says in these words:

Returns

True if the listener has consumed the event, false otherwise.


There's a difference between:

  • Implementing the View.OnKeyListener interface in your Activity class.

That allows your Activity to implement the functionality provided by the interface in your class, i.e., to declare to the world that your Activity knows how to handle that kind of event.

Please pay attention to the fact that I said "declare". Just because you declared that you know how to handle a task doesn't mean people will give that task to you, nor does it mean that you can generate such tasks by yourself. It's a good metaphor for the implements keyword in my opinion. Here the Activity "asks for a task".

Metaphors aside, technically, the Activity is defining a way to handle that event, but it can't generate that kind of event by itself.

  • setting the View callbacks to your Activity implementation

Using that, a View binds to a listener (which happens to be your Activity), promising to notify it whenever the event happens.

It "contracts" with your Activity to receive an input (the user presses the ENTER key while the View is in focus) and notifies the Activity. And since the Activity previously declared that it's capable of performing that, BOTH parties can execute the contract as previously agreed (see previous item).

Metaphors aside again, technically, here the Activity is registered by the View to be notified later when the View trigger the event. The Activity declares how, but the View knows when.

Conclusion:

This is just a metaphor for interfaces (at least in this case). It may look complicated, but it's crystal clear when you think of it as a two-party agreement. If you need a better, technical, explanation, I suggest reading about interfaces.


Answer to the new comment question:

Hello David and everyone else. Really I can't set a listener to the whole Activity?

Not in that way. You need to override dispatchKeyEvent. An example:

@Override
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
    Toast.makeText(UITestsActivity.this,
               "YOU CLICKED ENTER KEY",
                Toast.LENGTH_LONG).show();
        return true;
    }
    return super.dispatchKeyEvent(e);
};
like image 199
davidcesarino Avatar answered Nov 12 '22 02:11

davidcesarino