I am creating login screen with 2 editTexts: etUsername and etPassword.
On the etUsername, user should input the username and press Enter to go to the edit text etPassword, then he inputs the password, press Enter to login. Here is my current code:
etUsername.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_ENTER)) {
etPassword.requestFocus();
return true;
} else
return false;
}
});
etPassword.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_ENTER)) {
loginToServer();
return true;
} else
return false;
}
});
But when I input the username, and then press Enter – the program tries to log in to the server.
In the debug mode, I saw that when I pressed Enter once (on the etUsername) then first: etUsername.onKey() is called and then etPassword.onKey() is also called !
How can I modify the code so that the ENTER event is only processed once for the current field?
Before you check which key is pressed, try adding a check for which key event occurs. I would guess that it first triggers on the key-down event, then you move focus to the second text field, and here it also triggers on the key-up event.
See here for an example on how to check both the event type and key: Use "ENTER" key on softkeyboard instead of clicking button
I'm not 100% sure of this, but it is worth a try :-)
Related
When user click on the field and keyboard comes up, by pressing the Enter/Go key, I want to type my own value into the field, instead of system keyboard.
I want to do this in background of my app, and fill the fields of other apps.
Is it possible job, or I'm a greedy man?! :)
Yes it is possible, you are not greedy :P
You can add onKeyListener() on each of your EditText fields. It will listen to each key stroke by you user and as soon user press Enter, you can perform your required logic.
final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.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(MyExampleActivity.this, "enter is pressed", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
i have been using dispatchKeyEvent to get the keycode of android keyboard but the problem is when i tap on Sym or Emoji button nothing happen, no keycode shows. this is the code i use to get the keycode:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
Toast.makeText(getBaseContext(), "key pressed : "+ String.valueOf(event.getKeyCode()), Toast.LENGTH_SHORT).show();
return super.dispatchKeyEvent(event);
}
Use KeyEvent.KEYCODE_PICTSYMBOLS for emoji.
/*
* Respond to soft keyboard events, look for the DONE press on the password field.
*/
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KeyEvent.KEYCODE_PICTSYMBOLS))
{
// Done pressed! Do something here.
}
// Returning false allows other listeners to react to the press.
return false;
}
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 have been working on passing an edit text value to a string so I can upload the input to my database, but after a lot of time working I have figured out how to do this by pressing enter on the keyboard. I also have a button for uploading the string and I want to have only the button involved, so the upload button will be enter also.
Here is my button:
submit.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
postData();
}
});
Here is the key press enter command:
enter.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View view, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
name = enter.getText().toString();
return true;
}
return false;
}
});
Here is where I am using the converted string called name:
nameValuePairs.add(new BasicNameValuePair("username", name));
So I want to get rid of the need for the user to press enter before pressing the submit button, so pressing upload will take care of the enter key press code. I tried creating a method to simulate this being pressed but it didn't work, and researching didn't show me a possible way to assign a key press to a button on click.
Any suggestions would be appreciated.
Thanks.
you can try this to perform press button
enter.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View view, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
name = enter.getText().toString();
submit.performclick();
return true;
}
return false;
}
});
I have an Activity with two EditText fields. So when I press enter in the first one the second one becomes focused.
However I want to disable Enter key in the first EditText sometimes (for example when I think that the user haven't made a proper input into the first EditText).
I' ve overwriten onKeyDown for the 1st EditText returning true when key event is KEYCODE_ENTER but that doesn't help.
What shall I do?
How about adding an OnFocusChangeListener? When your EditText looses focus, you can do validation and can do a requestFocus for the "right" field.
Just by adding an editorActionListener:
editText1.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE || event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
if(isValidContent(editText1.getText().toString()){
editText2.requestFocus();
return true;
} else {
....
}
}
return false;
}
});