imeOptions "actionNext" programmatically - how to jump to next field?

Viewed 65051

In layout XML it is possible to specify android:imeOptions="actionNext" which adds Next button in virtual keyboard and by clicking on it - focus jumps to the next field.

How to do this programmatically - e.g. based on some event trigger focus to go to the next field?

10 Answers

The kotlin pendant

editText.imeOptions = EditorInfo.IME_ACTION_DONE
editText.setLines(1);
editText.setSingleLine(true);
editText.setImeOptions(EditorInfo.IME_ACTION_GO);

I solve the problem make sure with single line and go next editText when click enter

In my case, set an imeOptions fix the problem.

edtAnswer.maxLines = 1
edtAnswer.inputType = InputType.TYPE_CLASS_TEXT
edtAnswer.imeOptions = EditorInfo.IME_ACTION_NEXT

I tried all of the answers but EditorAction worked for me!

EditText.onEditorAction(EditorInfo.IME_ACTION_NEXT)

My XML layout:

  <EditText
            android:id="@+id/input1"
            style="@style/edittext"
            android:nextFocusForward="@id/input2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"/>
        <EditText
            android:id="@+id/input2"
            style="@style/edittext"
            android:nextFocusLeft="@id/input1"
            android:layout_weight="1"
            android:nextFocusRight="@id/input3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"/>

and Kotlin code:

input1.onEditorAction(EditorInfo.IME_ACTION_NEXT)

Now focus moves to the input2 Edittext.

You can do this by

edittext.imeOptions = EditorInfo.IME_ACTION_DONE //for done button

or

edittext.imeOptions = EditorInfo.IME_ACTION_NEXT //for next button

But... you need to understand that if you are using filters for edittext then you need to set

edittext.setSingleLine()

You can jump to the next field by using the following code:

BaseInputConnection inputConnection = new BaseInputConnection(editText, true);
inputConnection.performEditorAction(EditorInfo.IME_ACTION_NEXT);
//Use EditorInfo.IME_ACTION_UNSPECIFIED if you set android:imeOptions on the EditText
Related