Search code examples
androidkeydownonkeydownandroid-keypad

Android sign in key not working


I have created a login activity .In the login screen, the password editText comes with a keyboard that has "sign in" Key . When I press this key , the key board hides but no action takes place. I have overriden the onKeyDown() and assumed this key to be enter . But Still the login action does not take place

  @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

switch (keyCode) {
    case KeyEvent.KEYCODE_ENTER:
    {
        //your Action code
        if (DeviceUtil.isNetworkAvailable(LoginActivity.this)) {
            authenticate(muserName.getText().toString(),   mpwd.getText().toString());
        } else {
            showMessage("Network Not Available!");
        }
        return true;
    }
}
return super.onKeyDown(keyCode, event);

}

Can anyone please suggest what I can do here?


Solution

  • Set the onKeyListener to the view that's going to be focused when the key is pressed. For instance, if you're view has an id of password, you'd do the following:

    findViewById(R.id.password).setOnKeyListener(new View.OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                //Your code
                return true;
            }
            return false;
        }
    });