android:imeOptions not go to the exact next EditText - android

I have 2 EditText with id (et_1 and et_2) in a layout, and I set
for et_1
android:imeOptions="actionNext",
android:nextFocusForward="#+id/et_2"
when et_1 gets focus and the the software keyboard shows "Next", and when I click "Next", the focus doesn't go to et_2, but to another EditText in another fragment layout. Why is it this way?
How to make the focus go to et_2?
Thanks.

Try changing,
android:nextFocusForward="#+id/et_2"
to
android:nextFocusDown="#+id/et_2"
I hope it helps!

I had the same problem in one of my apps. What I did instead was have a listener in my activity for when "Next" was pressed and then just manually changed the focus. Like this:
final EditText et_1 = (EditText)findViewById(R.id.et_1);
final EditText et_2 = (EditText)findViewById(R.id.et_2);
// listen on the et_1 EditText
et_1.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_NEXT) { // "Next was selected"
et_2.requestFocus(); // focus on et_2
}
return true;
}
});

I realised referencing does not hide the keyboard
android:nextFocusDown="#+id/et_2"
To Hide the Keyboard add actionDone
<EditText
android:id="#+id/editTextEnterAmount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Amount"
android:imeOptions="actionDone"
/>

Related

onEditor action not working when trying to respond to the tick on the android numeric soft keyboard

I have an EditText in my app with an input type of number:
<EditText
android:id="#+id/answerText"
style="#style/GeneralTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/question"
android:layout_toRightOf="#id/equals"
android:inputType="number"
android:paddingTop="#dimen/activity_vertical_margin"
android:maxLength="3"
android:visibility="invisible"
tools:text="ans"/>
So when the EditText has focus the numeric keyboard is displayed. I am trying to repond to the user pressing the tick button after supplying a value in the EditText but I can't get it to work.
I've tried the following code which I've put inside the onCreate method but it isn't working:
answerGivenText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE){
return true;
}
return false;
}
});
Can anyone help please?
I think you should add the
android:imeOptions="actionDone"
attribute to your edittext.
That code is exactly what you need, but you are not doing anything inside it, you're just intercepting the click and returning true, indicating that you handled it to other listeners on the stack.
You would do whatever it is you want to do when the user clicks the tick in the
if (actionId == EditorInfo.IME_ACTION_DONE)
section. And turn on the DONE action in your layout. What exactly are you trying to achieve?

Move from Custom EditText to another Custom EditText when Soft Keyboard Next is clicked on Android

I've searched a lot of threads and nothing resolved my problem.
How can I go to another field by clicking "next button" on keyboard in that condition?
I've got "FieldValidLayout" with TextViews and "CustomEditText". After next button clicked on keyboard i would like to go to the "CustomEditText" in next "FieldValidLayout".
you can directly add in xml
android:imeOptions="actionNext"
or if you want more functions then
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//add your code
}

Android: Change "Done" button text on keyboard

I am new to Android development. Is it possible to change "Done" button's text on keyboard? If yes, how to do that?
EditText input = new EditText(context);
input.setImeOptions(EditorInfo.IME_ACTION_DONE);
input.setImeActionLabel("My Text", EditorInfo.IME_ACTION_DONE);
Alternatively you can do it in the XML
android:imeActionLabel="My Text"
you can set ImeOptions for textview in xml. their are many as per
this
Link for imeOptions
for example various imeOptions..
actionNone IME_ACTION_NONE.
actionGo IME_ACTION_GO.
actionSearch IME_ACTION_SEARCH.
actionSend IME_ACTION_SEND.
actionNext IME_ACTION_NEXT.
android:imeOptions="actionDone"
use like this:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionGo"
/>
and call this function to use action:
mInstrunctionEditTxt
.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_GO) {
// do what action you want
}
return false;
}
});

Android keyboard enter button to keep keyboard visible

I'd like for my enter key to complete an action without hiding the keyboard afterwards. Currently every time I enter something and press the enter button the EditText input is cleared to accept the next input, but as the keyboard disappears I have to click the EditText again so that the keyboard would appear again...
Right now this is on my layout:
<EditText
android:id="#+id/etCommand"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/commandHint"
android:inputType="textNoSuggestions"
android:imeOptions="actionSend" >
</EditText>
And the code:
etCommand = (EditText) findViewById(R.id.etCommand);
etCommand.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEND) {
sendCommand();
}
return false;
}
});
EDIT
As swayam suggested I had to return true inside onEditorAction so that the OS would think the action was handled and leave it alone.
Have a go at this code snippet :
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
Add the KeyListener for EditText for Enter key press and after the task completed requestfocus on edittext again would be solution

Recommendations on the use of an EditText

I need a way to take text from a "EditText" as soon as the user presses "Enter" and it should also call a method if it loses focus.
I created EditText like this:
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/button2"
android:ems="10" >
To receive key presses your EditText needs to have focus:
editText1.setFocusableInTouchMode(true);
editText1.requestFocus();
then set the onkeylistener():
editText1.setonKeyListener(new OnKeyListener(
#override onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_UP && keyCode==KeyEvent.KEYCODE_ENTER){
// what you need to do when Enter is pressed
return true;
}else return false;
}
For focus lost, you need to implement the View.onFocusChange interface and check that hasFocus is false
Use an EditText action listener. Refer to this question, it's probably the same: Handle Enter in EditText
For detecting loss in focus:
if(!(et.isFocused)){
//call your method here
}

Categories

Resources