In my app I need to display one or two edittexts to collect information (e1 and e2), depending on the selection the user will do through a radiobutton. This is done by setting the visibility state of the edittext to GONE and works fine.
My problem is how to programmatically set the IME_ACTION from "done" to "next" for each case, i.e.:
1) Only e1 is visible - set IME_ACTION of e1 to DONE
2) e1 and e2 are visible - set IME_ACTION of e1 to NEXT and IME_ACTION of e2 to DONE.
I'm using android:minSdkVersion="4" and android:targetSdkVersion="16" and testing on a Android 2.2 device.
Here's my layout:
<EditText
android:id="#+id/e1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLines="1"
android:singleLine="true"
android:imeOptions="actionDone"
android:hint="#string/sh15"
android:textColor="#android:color/black"
android:textSize="#dimen/s">
</EditText>
<EditText
android:id="#+id/e2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLines="1"
android:singleLine="true"
android:imeOptions="actionDone"
android:hint="#string/sh16"
android:textColor="#android:color/black"
android:textSize="#dimen/s">
</EditText>
Here's my code:
RadioGroup r= (RadioGroup) dialog.findViewById(R.id.rg);
r.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId)
{
switch(checkedId)
{
case R.id.rb1: //show one edittext
e1.setVisibility(View.VISIBLE);
e2.setVisibility(View.GONE);
e1.setImeOptions(EditorInfo.IME_ACTION_DONE);
break;
case R.id.rb2: //show two edittext
e1.setVisibility(View.VISIBLE);
e2.setVisibility(View.VISIBLE);
e1.setImeOptions(EditorInfo.IME_ACTION_NEXT);
e2.setImeOptions(EditorInfo.IME_ACTION_DONE);
break;
}
}
});
e2.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Overrid
public boolean onEditorAction(TextView v, int actionId,KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
//your code
}
}
)};
In case anyone lands here: EditText.setImeOptions(EditorInfo.IME_ACTION_NEXT); and god help you if it's TouchWiz :)
Also, I have noticed problems when the EditText can't switch if it has focus, so make sure to close the keyboard and rescind focus
Related
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
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.
I'm working on an Android app and I've got 2 editviews and a label. The user can enter 2 values and the label shows some calculation using input from the editviews. What I want is the following;
user enters either value with soft-keyboard
user presses "Return" softkey
editview should lose focus
the soft-keyboard should disappear
textview label should be recalculated
Now, the v.clearFocus only seems to works when there is another widget that can can get focus(?), so I've also added a dummie zero-pixel layout that can 'steal' the focus from the first editview. The Return key works now, but when the user switches focus from edit1 to edit2 by simply tapping then HideKeyboard() crashes. I've tried checking if inputMethodManager==null but that didn't help.
This all feels like I'm hacking to trick Android into doing some common UI behaviour, so I can't help but think that I'm overlooking something here.
Any help would be greatly appreciated. Btw I know this is similar to this question: How to lose the focus of a edittext when "done" button in the soft keyboard is pressed?
But I've tried that and it doesn't work.
So my layout xml is this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- Dummy control item so that first textview can lose focus -->
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<EditText
android:id="#+id/editTest1"
android:layout_width="250px"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:imeOptions="actionDone" >
</EditText>
<EditText
android:id="#+id/editTest2"
android:layout_width="250px"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:imeOptions="actionDone" >
</EditText>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test123" />
</LinearLayout>
And the source is this:
public class CalcActivity extends Activity implements OnFocusChangeListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab2_weight);
EditText testedit = (EditText) findViewById(R.id.editTest1);
testedit.setOnFocusChangeListener(this);
testedit.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
//Clear focus here from edittext
Log.d("test app", "v.clearFocus only works when there are other controls that can get focus(?)");
v.clearFocus();
}
return false;
}
});
}
public void hideSoftKeyboard() {
InputMethodManager inputMethodManager = (InputMethodManager) this.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
}
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus == false) {
Log.d("unitconverter", "onFocusChange hasFocus == false");
// update textview label
TextView bla = (TextView) findViewById(R.id.textView1);
bla.setText(String.format("%s + %s", (((EditText) findViewById(R.id.editTest1)).getText()), (((EditText) findViewById(R.id.editTest2)).getText())));
// hide keyboard
hideSoftKeyboard();
}
}
}
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.
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"