I have been trying to capture the Enter key for the past couple of hours but I donot get a call in the Listener. I am trying to provide a "Press Enter to send". But am always registering for the OnKeyListener. A few keys get called but randomly.
Its not a duplicate, since all the answers provide the same set of details, and this is a standard code. I am using the nexus 6P
My Code:
public void onCreate(){
//set press enter to save state
anEditText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int keyCode, KeyEvent event) {
if( keyCode ==
EditorInfo.IME_ACTION_SEND || event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN){
if (new SharedPrefsManager(mContext).getBoolean(StringConstants.KEY_PRESS_ENTER_TO_SAVE, false)) {
Toast.makeText(mContext, "Enter pressed, to save", Toast.LENGTH_SHORT).show();
}
return true;
}
return false;
}
});
}
@OnCheckedChanged(R.id.check_enter_to_send)
public void enterToSendChecBoxClicked(CompoundButton buttonView, boolean isChecked){
// 1. set the preference
new SharedPrefsManager(mContext).putBoolean(StringConstants.KEY_PRESS_ENTER_TO_SAVE, isChecked);
}