111

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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Laimoncijus
  • 8,615
  • 10
  • 58
  • 81

11 Answers11

219

You can use the constants from EditorInfo class for the IME options. like,

editText.setImeOptions(EditorInfo.IME_ACTION_NEXT);
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
MSD
  • 2,627
  • 1
  • 18
  • 12
31

Search for the next focusable field and than invoke requestFocus().

TextView nextField = (TextView)currentField.focusSearch(View.FOCUS_RIGHT);
nextField.requestFocus();
Justin
  • 20,509
  • 6
  • 47
  • 58
  • 2
    This is not a true answer in reference to the title of this question, but it is technically a valid answer. Below answer is more appropriate. – Sean Glover Sep 14 '12 at 19:37
  • 5
    @SeanGlover you should never use words like ***below*** because the position of answers keep on changing. – Nike15 Dec 27 '16 at 10:09
29

Just suggestion, if you are using

     EditTextSample.setImeOptions(EditorInfo.IME_ACTION_DONE); 

it doesn't work, make sure that your EditText is using a single line.

Eg:

       editTextSample.setSingleLine();
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
ziniestro
  • 686
  • 1
  • 11
  • 24
7

There is always necessity to add extra keys apart from default keys available in virtual QWERTY keyboard.

Using XML

<EditText android:text="@+id/EditText01" 
android:id="@+id/EditText01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:imeOptions="actionDone"/>

By Programmatic Way

An EditorInfo is most useful class when you have to deal with any type of user input in your Android application.

IME_ACTION_DONE: This action performs a “done” operation for nothing to input and the IME will be closed.

 EditTextSample.setImeOptions(EditorInfo.IME_ACTION_DONE);

For more information you may visit http://developer.android.com/reference/android/view/inputmethod/EditorInfo.html

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
1

The kotlin pendant

editText.imeOptions = EditorInfo.IME_ACTION_DONE
kuzdu
  • 7,124
  • 1
  • 51
  • 69
1
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

Sterling Archer
  • 22,070
  • 18
  • 81
  • 118
Handelika
  • 198
  • 3
  • 11
1

In my case, set an imeOptions fix the problem.

edtAnswer.maxLines = 1
edtAnswer.inputType = InputType.TYPE_CLASS_TEXT
edtAnswer.imeOptions = EditorInfo.IME_ACTION_NEXT
Mete
  • 2,805
  • 1
  • 28
  • 38
1

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()
Kishan Solanki
  • 13,761
  • 4
  • 85
  • 82
1

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.

Siamak
  • 1,689
  • 1
  • 17
  • 26
0

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
Pierre
  • 8,397
  • 4
  • 64
  • 80
0

With Android Compose, we simply need to add the keyboard options:

// inside a @Composable function
val imeNextAction = KeyboardOptions.Default.copy(
    imeAction = ImeAction.Next
)
OutlinedTextField(
    modifier = Modifier.fillMaxWidth(),
    value = emailOrMobileCheckin.city,
    onValueChange = {},
    label = {
        Text(text = "City")
    },
    keyboardOptions = imeNextAction
)
OutlinedTextField(
    modifier = Modifier.fillMaxWidth(),
    value = emailOrMobileCheckin.state,
    onValueChange = {},
    label = {
        Text(text = "State")
    },
    keyboardOptions = imeNextAction
)
OutlinedTextField(
    modifier = Modifier.fillMaxWidth(),
    value = emailOrMobileCheckin.country,
    onValueChange = {},
    label = {
        Text(text = "Country")
    },
    keyboardOptions = imeNextAction
)
Aakash
  • 21,375
  • 7
  • 100
  • 81