How to detect when “Go” is pressed on Android Keyboard

I’m trying to detect when “Go” is pressed on the android Keyboard (the last key on the bottom right).
My code works in the emulator and on my Nexus 5, but doesn’t on some other Android devices.
What am I doing wrong?

editTextMain.setImeActionLabel("Go", KeyEvent.KEYCODE_ENTER);
editTextMain.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
         if(i==KeyEvent.KEYCODE_ENTER)
         {
            Intent intent = new Intent(thisActivity, ActivityRisultati.class);
            startActivity(intent);
            return true;
         }
    }

}

Answer

Try this

editText = (EditText) findViewById(R.id.edit_text);

editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            // do your stuff here
        }
        return false;
    }
});

Leave a Reply

Your email address will not be published. Required fields are marked *