1

We enter value in first EditText. Now I want when we press done button then cursor move to next EditText.

I tried to do it and search on Google but nothing found.

Lucifer
  • 29,392
  • 25
  • 90
  • 143
Sarbjyot
  • 136
  • 2
  • 8
  • Possible duplicate of [Android XML - moving between EditText fields](https://stackoverflow.com/questions/16223852/android-xml-moving-between-edittext-fields) – Ricardo A. May 03 '19 at 12:14

5 Answers5

3

You can get DONE button click and set focus to next EditText

first_edit_text.setOnEditorActionListener(new OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                 if(actionId == 0 || actionId== EditorInfo.IME_ACTION_DONE)
                 {
                     next_edit_text.requestFocus();
                 }
                return false;
            }
      });

EDIT :

To fulfill your requirement :

public void setEditText() {
        edittext = new EditText[length];
        for (int i = 0; i < length; i++) {
            edittext[i] = new EditText(this);
            edittext[i].setId(i);
            edittext[i].setSingleLine();
            edittext[i].setImeOptions(EditorInfo.IME_ACTION_DONE);
            edittext[i].setOnEditorActionListener(new OnEditorActionListener() {

                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId == 0 || actionId == EditorInfo.IME_ACTION_DONE) {

                        int current_id= v.getId();
                        if((current_id+1)!=length){
                                 edittext[current_id+1].requestFocus();
                        }
                    }
                    return false;
                }
            });
            linear.addView(edittext[i]);
        }
    }
SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
  • Edit text which used in this app is created programatically so what we do – Sarbjyot Mar 06 '14 at 07:30
  • public boolean onEditorAction(TextView arg0, int keyCode, KeyEvent event) { EditText edit_text = (EditText) arg0; sb.append(edit_text.getText().toString().trim()); edit_text.setSingleLine(); edit_text.setImeOptions(EditorInfo.IME_ACTION_DONE); – Sarbjyot Mar 06 '14 at 08:47
  • First edittext is created or not?? You just have to add EditorActionListener on it.. – SweetWisher ツ Mar 06 '14 at 08:55
  • we create edittext using for loop public void setEditText() { edittext = new EditText[length]; for (int i = 0; i < length; i++) { edittext[i] = new EditText(this); edittext[i].setId(i); edittext[i].setSingleLine(); edittext[i].setImeOptions(EditorInfo.IME_ACTION_DONE); edittext[i].setOnEditorActionListener(this); linear.addView(edittext[i]);}} – Sarbjyot Mar 06 '14 at 09:11
  • OK so what's the issue now?? And put your code in pastie.org so atleast readable to all – SweetWisher ツ Mar 06 '14 at 09:15
  • we also get the value but when we fill the first edit text then it value get when we click on done button on soft keyboard. this process repeat for all edit text. And i want when we fill first edit text and click done the the keyboard automatically open we just fill value and click done. – Sarbjyot Mar 06 '14 at 09:42
  • Sorry but Can't get what you wanna say :( – SweetWisher ツ Mar 06 '14 at 09:49
  • @ SweetWhiser Your code working for me but a little issue is that when we fill value in last edit text and press done the app will be crashed . I think according to your this logic :- int current_id= v.getId(); if(current_id!=length) ((EditText)findViewById(current_id+1)).requestFocus(); when we reach last edittext the can not find next edit text. – Sarbjyot Mar 07 '14 at 05:44
  • Ya you need to handle that case dear...write else block accordingly – SweetWisher ツ Mar 07 '14 at 05:50
  • Check edited answer.I have changed the if condition...Now it will work..And dont forget to accept the answer if it worked for you..Best luck :) – SweetWisher ツ Mar 07 '14 at 05:54
2

Please use imeOptions in EditText.

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="32dp"
    android:layout_marginTop="16dp"
    android:imeOptions="actionNext"
    android:ems="10" >
    <requestFocus />
</EditText>
Pratap
  • 591
  • 8
  • 25
2

You can try triggering Next key's click in onClick of DONE key as shown here:

editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            Activity.dispatchKeyEvent(KeyEvent.KEYCODE_NEXT); //trigger Next key's click here 
        }
        return false;
    }
});

Hope that helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
2

After entering the value in the text, to go to next edit box you can type in the xml layout editbox code as following:

android:nextFocusDown="@+id/here your next edit box id"
Bram
  • 2,515
  • 6
  • 36
  • 58
user3811671
  • 151
  • 1
  • 8
0

If you place android:singleLine="true" on your EditText it will automatically change the Enter button on your keyboard into a Next button.

Naskov
  • 4,121
  • 5
  • 38
  • 62