I am trying to get a login screen for an Android app and so far this is my code:
Just add android:inputType="..."
to your EditText. It will work!! :)
You should set OnEditorActionListener for the EditText to implement the action you want to perform when user clicks the "Done" in keyboard.
Thus, you need to write some code like:
password.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// Do whatever you want here
return true;
}
return false;
}
});
See the tutorial at android developer site
just Add this to your attributes :
android:inputType="textPassword"
Doc: here
Qianqian is correct. Your code only listens for the button click event, not for the EditorAction event.
I want to add that some phone vendors may not properly implement the DONE action. I have tested this with a Lenovo A889 for example, and that phone never sends EditorInfo.IME_ACTION_DONE
when you press done, it always sends EditorInfo.IME_ACTION_UNSPECIFIED
so I actually end up with something like
myEditText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event)
{
boolean handled=false;
// Some phones disregard the IME setting option in the xml, instead
// they send IME_ACTION_UNSPECIFIED so we need to catch that
if(EditorInfo.IME_ACTION_DONE==actionId || EditorInfo.IME_ACTION_UNSPECIFIED==actionId)
{
// do things here
handled=true;
}
return handled;
}
});
Also note the "handled" flag (Qianqian didn't explain that part). It might be that other OnEditorActionListeners higher up are listening for events of a different type. If your method returns false, that means you didn't handle this event and it will be passed on to others. If you return true that means you handled/consumed it and it will not be passed on to others.
Just to add to Qianqian & peedee answers:
Here's the reason why the action id is EditorInfo.IME_ACTION_UNSPECIFIED (0) for any enter event.
actionId int: Identifier of the action. This will be either the identifier you supplied, or EditorInfo#IME_NULL if being called due to the enter key being pressed.
(Source: Android documentation)
So the proper way to handle enter key events would be:
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_NULL) {
// Handle return key here
return true;
}
return false;
}
After trying many things, this is what worked for me:
android:maxLines="1"
android:inputType="text"
android:imeOptions="actionDone"