I'm making a dictionary. I want when I press "Enter", the EditText doesn't insert new line and do search instead. How to do it?
I tried OnKey, OnKeyDown, OnEditorAction but it doesn't work. When I press "Enter", EditText inserts a new line.
public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event) {
System.out.println("OnEditor.");
if (actionId == EditorInfo.IME_ACTION_SEARCH ||
actionId == EditorInfo.IME_ACTION_DONE ||
event.getAction() == KeyEvent.ACTION_DOWN &&
event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
System.out.println("OnEditor.");
}
return true;
}
Update: imeOption doesn't works.
Instead of the deprecated singleLine attribute you are supposed to use
android:inputType="text"
to force single line input.
You can also restrict edittext to single line using
android:singleLine="true"
You need to set the android:imeOptions attribute in your XML which will show the appropriate keyboard button. Then you can listen for the onEditorAction call.
add the following line to your edit text
android:imeOptions="actionSend|flagNoEnterAction"
Related
I have a specific requirement for providing every user a custom button that will only be available for him on the keyboard. The Custom Button can be any like (%,#,#,etc or may be numeric). How can i add that on the Custom Keyboard any ideas. I have already added the custom keyboard in my app but i also need this button.
Thanks in advance.
Actually you can set custom text to that little blue button. In the xml file just use
android:imeActionLabel="userName"
You can do this programatically as well like
etEditText.setImeActionLabel("userName", EditorInfo.IME_ACTION_DONE);
And if you what the Button onClickListner than use
etEditText.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
Log.i(TAG,"Enter pressed");
}
return false;
}
});
It should be noted that this will not cause text to appear on all keyboards on all devices. Some keyboards do not support text on that button (e.g. swiftkey). And some devices don't support it either. A good rule is, if you see text already on the button, this will change it to whatever you'd want.
so i have a login form. When i press the back button or the enter button, it seems to go the next edit text (or previous) even though i set these following options for my edit text.
What is weird, once i cycle through them all, everything works fine as it should i.e. back button / done button dismiss the edit text and key board.
Please note i am only showing 1 edit text field to not flood the post. The rest are pretty much the same
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<com.test.views.widgets.myEditText
android:id="#+id/userFirstName"
android:imeOptions="actionDone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/generic.first.name"
android:inputType="textPersonName"
android:textColor="#android:color/white"
android:theme="#style/EditText.Base.Style"
app:font="regular" />
</android.support.design.widget.TextInputLayout>
So the above, i have set actionDone which was one of the suggestions to this problem. This does not work.
In addition, I am extending the AppCompatEditText to have my own implementation.
Here is some code
When done button is pressed
#Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if(actionId == EditorInfo.IME_ACTION_DONE){
clearFocus();
}
return false;
}
When the back button is pressed
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
clearFocus();
}
return false;
}
Also note that i followed this tutorial as a means to hide keyboard when focus changes
http://toastdroid.com/2015/06/04/on-screen-keyboard-tracking-part-2/
Add android:imeOptions="actionDone". It will close keyboard rather than going on next focusable view.
add this to the EditText attribute
android:maxLines="1"
Try adding android:imeOptions="actionDone" to your EditText
OR
youredittext.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int keyCode,
KeyEvent event) {
if ( (event.getAction() == KeyEvent.ACTION_DOWN ) &&
(keyCode == KeyEvent.KEYCODE_ENTER) )
{
// hide virtual keyboard
InputMethodManager imm =
(InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);
return true;
}
return false;
}
});
I suppose that problem is in your clearFocus(). Documentation says:
Note: When a View clears focus the framework is trying to give focus
to the first focusable View from the top. Hence, if this View is the
first from the top that can take focus, then all callbacks related to
clearing focus will be invoked after which the framework will give
focus to this view.
try to replace clearFocus() with hideKeyboard() like this
void hideKeyboard() {
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(yourView.getWindowToken(), 0);
}
I managed to fix this.
I added this to my root layout (which contained the edit text) and the problem no longer seems present
android:focusableInTouchMode="true"
I am facing an issue, I have username & password fields on activity, now when I click on username keyboard appears but no next button on it and I cannot move to next Edittext control through keyboard in this case, keyboard displays enter button in it as attached in screenshot which increases its height,
Can anyone guide me what is the solution to this problem (to display next button on edittext)?
My Code
txtUserid = (EditText)findViewById(R.id.txtUserID);
txtUserPasword = (EditText)findViewById(R.id.txtPassword);
txtUserid.setNextFocusDownId(R.id.txtPassword);
txtUserPasword.setNextFocusDownId(R.id.btnLogin);
Add android:singleLine="true" android:nextFocusDown="#+id/textView2" on your xml.
Will show Next key and also focus to the next field.
In your layout, just set the XML attributes android:imeOptions="actionNext" for your first three text boxes and android:imeOptions="actionDone" for the last one.
More Examples
add this lines below your lines of code you provided:
txtUserid.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))
{
// Perform action on Enter key press
txtUserid.clearFocus();
txtUserPasword.requestFocus();
return true;
}
return false;
}
});
txtUserPasword.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER))
{
// Perform action on Enter key press
// check for username - password correctness here
return true;
}
return false;
}
});
Another best solution would be:
android:singleLine="true"
android:imeOptions="actionNext"
If your EditText is supposed to have only 1 line, add the attribute android:singleLine="true" on your XML, that will remove the Enter key and replace it with Next / Done button.
add your xml layout ,
in suppose u want 4 edittext in row so first three editext u set below,
android:imeOptions="actionNext"
and last one editext u set
android:imeOptions="actionDone"
u add line so add,Use
android:singleLine="true"
You need to take care of few things:
Add android:singleLine="true".
Add android:nextFocusDown="#+id/tv2" on your XML.
Don't add android:imeOptions="actionNext", otherwise it will override above two attributes.
use this attribute:
android:inputType="textPersonName"
Add
android:inputType="text"
line in your edittext tag. and your problem will be resolved.
Just add android:singleLine="true" to the EditText tag in the layout XML..
Just add android:singleLine="true" you don't need that code for that
android:singleLine="true" is deprecated
use
android:maxLines="1"
android:inputType="text"
instead
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;
}
});
In my login form when user clicks on an EditText and presses the enter key, this inserts a new line, therefore increasing the EditText's size. Next moment, it returns to its previous place and prints a dot in the password field (which is the next field).
I want to remove this enter key from the softkeyboard. Is it possible?
Use :
android:singleLine = "true"
or
edittext.setSingleLine();
And your ENTER key is gone
add this tag to textView in xml
android:singleLine = "true"
I am afraid you can't do this. But one thing is you can handle the softkeyboard keyevents like this,
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
Log.i("event", "captured");
return false;
}
else if(event.getAction() == KeyEvent.ACTION_DOWN
&& event.getKeyCode() == KeyEvent.KEYCODE_BACK){
Log.i("Back event Trigered","Back event");
}
}
}
return false;
}
});
Apart from this, you have to note that providing the attribute android:singleLine=true will make your edittext from growing in size when the soft keyborad ENTER is pressed
Inside the tag EditText you only have to do:
android:singleLine="true"
this remove the enter key in the keyboard
UPDATE
Inasmuch as android:singleLine="true" is deprecated I use android:maxLines="1" to avoid the enter in a EditText. How the name of the method says only N lines is permitted.
New update :
android:maxLines="1"
If you want something more generic in your .java:
boolean state = true;
yourTextInputEditText.setSingleLine(state);