KeyCode_Enter to next edittext - android

In edittext, after typing 'Enter' key, system make a new line inside it. I'd like to focus on next edittext, no new line. how to code? my code in xml is below
<EditText
android:id="#+id/txtNPCode"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_alignLeft="#+id/lblNPCode"
android:layout_below="#+id/lblNPCode"
android:layout_centerHorizontal="true"/>
<EditText
android:id="#+id/txtCNPCode"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_alignLeft="#+id/lblCNPCode"
android:layout_below="#+id/lblCNPCode"
android:layout_centerHorizontal="true"/>
I also caputer key code in setOnKeyListener
tCNPCode.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == 66) {
Toast.makeText(S_PCode.this, "Enter Key", Toast.LENGTH_LONG).show();
//tNPCode.setFocusable(true);
}
return false;
}
});

You don't even have to use OnKeyListener. Simply add line android:singleLine="true" to your EditText. After this operation EditText behaves exactly like you want. So, in your particular case:
<EditText
android:id="#+id/txtNPCode"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_alignLeft="#+id/lblNPCode"
android:layout_below="#+id/lblNPCode"
android:layout_centerHorizontal="true"
android:singleLine="true"
/>
<EditText
android:id="#+id/txtCNPCode"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_alignLeft="#+id/lblCNPCode"
android:layout_below="#+id/lblCNPCode"
android:layout_centerHorizontal="true"
/>
solves the problem.

You have to use the requestFocus method. In your case it will be something like:
tCNPCode.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_ENTER) {
txtCNPCode.requestFocus();
}
return false;
}
});

The best way is to use one of the options for inputType, for example: android:inputType="textPersonName"
This will achieve the wanted effect. Check out the other options too, it'll save you some time in future :)
You shouldn't use android:singleLine="true", since it's deprecated. Here's what the documentations says about it:
This attribute is deprecated and is replaced by the textMultiLine
flag in the inputType attribute. Use caution when altering existing
layouts, as the default value of singeLine is false (multi-line
mode), but if you specify any value for inputType, the default is
single-line mode. (If both singleLine and inputType attributes are
found, the inputType flags will override the value of singleLine.).

Consider IME options, good description here http://androidcookbook.com/Recipe.seam;jsessionid=C824437B48F346E23FBE5828969029B4?recipeId=422 and documentation here http://developer.android.com/reference/android/widget/TextView.html#attr_android:imeOptions.
The only thing you need is add attribute android:imeOptions="actionNext" on each EditText. Keyboard appears with a Next key where Enter used to be and allows to navigate to the next EditText.
But order of "actionNext" is from top to bottom. For example, if you have horizontal layout:
<LinearLayout android:orientation="horizontal" >
<EditText android:id="#+id/et1" android:imeOptions="actionNext" />
<EditText android:id="#+id/et2" android:imeOptions="actionNext" />
</LinearLayout>
et2 never get focus. You should fix it in the code:
et1.setOnEditorActionListener(new OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event)
{
switch (actionId)
{
case EditorInfo.IME_ACTION_NEXT:
boolean result = false;
TextView v1 = (TextView) v.focusSearch(View.FOCUS_RIGHT);
if (v1 != null)
result = v1.requestFocus(View.FOCUS_RIGHT);
else if (!result)
{
v1 = (TextView) v.focusSearch(View.FOCUS_DOWN);
if (v1 != null)
result = v1.requestFocus(View.FOCUS_DOWN);
}
if (!result)
v.onEditorAction(actionId);
break;
default:
v.onEditorAction(actionId);
break;
}
return true;
}
});
Original link: https://groups.google.com/forum/?fromgroups=#!topic/android-developers/OykOG6XtE8w

Use Single Line of Code (Java)
tCNPCode.setSingleLine(true);
Kotlin
tCNPCode.isSingleLine = true
It will navigate to the next item on Enter press

Just set the maximum number of line to the EditText box in xml layout file:
android:maxLength="1"

Related

setOnEditorActionListener not working for edit texts next to each other end of layout

here' the code
I have two edit Text in a row in my lay out,and when I touch next on keyboard it does not change the focus.
the setOnEditorActionListener does not work till i add extra editText
<EditText
android:id="#+id/firstText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:imeOptions="actionNext"
android:inputType="numberDecimal"
/>
<EditText
android:id="#+id/secondText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:imeOptions="actionDone"
android:inputType="numberDecimal"
/>
code:
firstText.setOnNextActionListener(new CallBack<KeyEvent>() {
#Override
public void call(KeyEvent data) {
secondText.requestEditTextFocus();
}
});
Give one value into edittext in xml file ..
android:singleLine="true"
You can try these
android:nextFocusDown="#+id/secondText"
android:nextFocusLeft="#+id/secondText"
android:nextFocusRight="#+id/secondText"
android:nextFocusUp="#+id/secondText"
Show below three ways, you can got you want Using any one of them
inputType ="actionNext" attribute
or
android:imeOptions="actionNext"
or
android:singleLine="true" and
android:nextFocusDown="#+id/secondText"
on your .xml you can do this.
If you want to do it programmatically in your activity or fragment then try below:-
firstText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event){
if (actionId == EditorInfo.IME_ACTION_NEXT) {
secondText.requestEditTextFocus();
return true; // Focus will do whatever you put in the logic.
}
return false; // Focus will change according to the actionId
}
});
I have tested with set Programmtically with changes secondText.requestEditTextFocus(); to secondText.requestFocus(); and its working fine.
Please show below Screenshotes

android:digits not allowing next button in keypad

Im using
android:digits="abcdefghijklmnopqrstuvwxyz. "
and having one more EditText in the same screen when i press enter key in keypad the focus doesn't gets changed.
suppose if i remove the
android:digits
the enter button works fine and moves to the next edit text.
Add android:imeOptions="actionNext" in your EditText in xml
<EditText
android:id="#+id/et_count"
android:layout_width="100dp"
android:singleLine="true"
android:imeOptions="actionNext"
android:layout_height="wrap_content" />
You can make use of the imeOptions attribute available for EditText in xml.
Try the following. It worked in my case.
In XML:
<EditText
android:id="#+id/edit_text1"
android:layout_width="match_parent"
android:layout_height="45dp"
android:digits="abcdefghijklmnopqrstuvwxyz. "
android:imeOptions="actionNext"/>
<EditText
android:id="#+id/edit_text2"
android:layout_width="match_parent"
android:layout_height="45dp"/>
Then, in JAVA:
EditText edit_text1 = (EditText) findViewById (R.id.edit_text1);
EditText edit_text2 = (EditText) findViewById (R.id.edit_text2);
edit_text1.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_NEXT) {
edit_text2.requestFocus();
handled = true;
}
return handled;
}
});
you must be delete 'android:digits' to active the 'android:imeOptions="actionNext"' code.

EditText moving long text to next line

I have an EditText in my app and I want it to have two lines, to show ime button instead of enter key and to move too long text to next line (like in sms apps). For now i have something like this:
<AutoCompleteTextView
android:id="#+id/name_field"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:background="#null"
android:freezesText="true"
android:hint="#string/some_hint"
android:imeOptions="actionNext"
android:maxLength="100"
android:nextFocusDown="#null"
android:lines="2"
android:ellipsize="end"
android:inputType="textImeMultiLine"
android:selectAllOnFocus="true"
android:textColor="#android:color/black"
android:textSize="16sp" />
It has two first properties I mentioned, but i can't recall any option that allows me to reach third.
For example: if one line in my EditText has 10 chars, i want to display text "abc abcd abc abcdefghijk" like that:
abc abcd
abc abc...
EDIT:
It seems problem is in android:inputType="textImeMultiLine". When i changed it to android:inputType="textMultiLine" all works fine, but... I have enter button instead of IME button, which i want to avoid.
Try this..Hope it will work!
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:background="#null"
android:freezesText="true"
android:imeOptions="actionNext"
android:maxLength="100"
android:nextFocusDown="#null"
android:lines="2"
android:ellipsize="end"
android:selectAllOnFocus="true"
android:textColor="#android:color/black"
android:textSize="16sp" />
add this line
android:inputType="textMultiLine"
add this in your xml android:maxLines="2" instead of android:lines="2"
None of these Methods worked for me.
After Googling for about an hour, I found this piece of code:
EditText editText = findViewById(R.id.editNote);
editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
editText.setImeActionLabel("DONE",EditorInfo.IME_ACTION_DONE); //Set Return Carriage as "DONE"
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if (event == null) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// Capture soft enters in a singleLine EditText that is the last EditText
// This one is useful for the new list case, when there are no existing ListItems
editText.clearFocus();
InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
else if (actionId == EditorInfo.IME_ACTION_NEXT) {
// Capture soft enters in other singleLine EditTexts
} else if (actionId == EditorInfo.IME_ACTION_GO) {
} else {
// Let the system handle all other null KeyEvents
return false;
}
}
else if (actionId == EditorInfo.IME_NULL) {
// Capture most soft enters in multi-line EditTexts and all hard enters;
// They supply a zero actionId and a valid keyEvent rather than
// a non-zero actionId and a null event like the previous cases.
if (event.getAction() == KeyEvent.ACTION_DOWN) {
// We capture the event when the key is first pressed.
} else {
// We consume the event when the key is released.
return true;
}
}
else {
// We let the system handle it when the listener is triggered by something that
// wasn't an enter.
return false;
}
return true;
}
});
Keep your EditText like this:
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/editNote"
android:hint="Start Typing..."
android:inputType="textMultiLine"
android:gravity="start" />
I don't remember where I got this code from so can't credit the author.
I made the necessary changes according to my need.

Set next EditText focused and editable on KEY_DOWN

I've got a layout with two editexts. I need to set the second one focused and editable when the enter key is hit on the first one. I've got code for setting focus but I can't start typing when the second one gets the focus.
PS I also need edittext to be exactly one line height without possibility of making addtional rows. It works as well.
pss android 2.3
XML code:
<EditText
android:id="#+id/email"
style="#style/profileLoginInput"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="#string/email" android:ems="10" android:lines="1" android:maxLines="1" android:ellipsize="end">
<requestFocus />
</EditText>
<EditText
android:id="#+id/password"
style="#style/profileLoginInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:inputType="textPassword" android:hint="#string/password">
Java code :
((EditText) findViewById(R.id.email)).setOnKeyListener(new OnKeyListener(){
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
((EditText) findViewById(R.id.password)).requestFocus();
return true;
default:
break;
}
}
return false;
}
});
You're overthinking this. You can specify the order of the EditText inputs right in the XML without doing anything in Java.
Use the android:nextFocusDown param to specify which field you'd like to get focus when the user hits the enter key.
android:inputType="text"
android:nextFocusDown="#+id/..."
worked for me
android:nextFocusDown
Worked fine for me
But removed the following Method only then its work:
editText1.setOnEditorActionListener(new EditText.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE ||
actionId == EditorInfo.IME_ACTION_NEXT) {
editText2.requestFocus();
return false;
}
return false;
}
});
Besides adding the + in front of id:
android:inputType="text"
android:nextFocusDown="#+id/..."
I also had to add:
android:imeOptions="actionNext"
It seems to be bugged when running this code on the emulator. However, it works on phones

Set carat/cursor from edittext1 to edittext2

I've already searched how to do this but couldn't find any solution. Here it goes, I have edittext1 and edittext2, while the carat/cursor is positioned in edittext1 then I pressed "Next/Enter" key in soft keyboard, the carat/cursor must be positioned in edittext2. The snippet below received the event when I pressed the "Next/Enter" key but didn't move the carat/cursor in edittext2.
edittext1.setOnKeyListener(new View.OnKeyListener()
{
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if(event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
{
Editable e = edittext2.getText();
Selection.setSelection(e,e.length());
}
return false;
}
});
Any inputs will be highly appreciated. Thanks.
Rather than doing it in code, can't you use the android:nextFocusDown and android:nextFocusUp in your XML? Here are some references:
Handling UI Events
View documentation
EDIT
With your accepted answer it looks like you have a solution that works, however I thought I'd get the XML route to work too. So here is a working version of your layout elements:
<AutoCompleteTextView
android:id="#+id/autoCompleteTextViewRecipient"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:completionThreshold="1"
android:inputType="text"
android:maxLines="1"
android:hint="To"
android:imeOptions="actionNext"
android:nextFocusDown="#+id/editTextComposeMessage"
android:nextFocusUp="#+id/editTextComposeMessage"/>
<EditText
android:id="#+id/editTextComposeMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
android:nextFocusDown="#+id/autoCompleteTextViewRecipient"
android:nextFocusUp="#+id/autoCompleteTextViewRecipient"/>
Differences are that the AutoCompleteTextView now has a nextFocusDown, the EditText has a nextFocusUp, and both have imeOptions set to actionNext
Try using this:
if(event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
{
edittext2.requestFocus();
}
The cursor moves to edittext2.

Categories

Resources