63

I am facing an issue, I have username & password fields on activity, now when I click on username keyboard appears but no next button on it and I cannot move to next Edittext control through keyboard in this case, keyboard displays enter button in it as attached in screenshot which increases its height,

Can anyone guide me what is the solution to this problem (to display next button on edittext)?

enter image description here

My Code

txtUserid = (EditText)findViewById(R.id.txtUserID);
        txtUserPasword = (EditText)findViewById(R.id.txtPassword);

        txtUserid.setNextFocusDownId(R.id.txtPassword);
        txtUserPasword.setNextFocusDownId(R.id.btnLogin);
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
UMAR-MOBITSOLUTIONS
  • 77,236
  • 95
  • 209
  • 278

12 Answers12

109

Add android:singleLine="true" android:nextFocusDown="@+id/textView2" on your xml. Will show Next key and also focus to the next field.

nijas
  • 1,879
  • 2
  • 15
  • 13
  • 3
    This works for me, although it might be important to note that if you set android:imeOptions="actionNext", then it will override the attributes mentioned in this answer – user1549672 Aug 22 '13 at 21:32
  • 2
    If you use android:singleLine="true" means, that itself good enough to visible the "next" button on the keypad – prabu Feb 28 '14 at 05:44
  • 3
    It works for me when also I get rid of `android:imeOptions="actionNext"` – Mr.Moustard Oct 29 '14 at 09:58
59

In your layout, just set the XML attributes android:imeOptions="actionNext" for your first three text boxes and android:imeOptions="actionDone" for the last one.

More Examples

Sky Kelsey
  • 19,192
  • 5
  • 36
  • 77
deepak Sharma
  • 1,641
  • 10
  • 23
40

add this lines below your lines of code you provided:

txtUserid.setOnKeyListener(new OnKeyListener() {

    public boolean onKey(View v, int keyCode, KeyEvent event) {
          // If the event is a key-down event on the "enter" button
          if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
               (keyCode == KeyEvent.KEYCODE_ENTER))
          {
                // Perform action on Enter key press
                txtUserid.clearFocus();
                txtUserPasword.requestFocus();
                return true;
          }
          return false;
    }
});

txtUserPasword.setOnKeyListener(new OnKeyListener() {

    public boolean onKey(View v, int keyCode, KeyEvent event) {

          if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                    (keyCode == KeyEvent.KEYCODE_ENTER))
          {
                 // Perform action on Enter key press
                 // check for username - password correctness here
                 return true;
          }
          return false;
    }
});
Hiral Vadodaria
  • 19,158
  • 5
  • 39
  • 56
9

Another best solution would be:

android:singleLine="true" 
android:imeOptions="actionNext"
Naveed Ahmad
  • 6,627
  • 2
  • 58
  • 83
Harsh Mittal
  • 2,868
  • 1
  • 16
  • 21
  • "next" produces an error: "Error:(48, 45) String types not allowed (at 'imeOptions' with value 'next').". It should be "actionNext". And android:singleLine="true" is deprecated. Use android:maxLines="1" instead. – jk7 Mar 02 '17 at 00:39
7

If your EditText is supposed to have only 1 line, add the attribute android:singleLine="true" on your XML, that will remove the Enter key and replace it with Next / Done button.

SERPRO
  • 10,015
  • 8
  • 46
  • 63
5

add your xml layout ,

in suppose u want 4 edittext in row so first three editext u set below,

android:imeOptions="actionNext"

and last one editext u set

android:imeOptions="actionDone"

u add line so add,Use

  android:singleLine="true" 
Kunal Dharaiya
  • 121
  • 1
  • 3
4

You need to take care of few things:

  1. Add android:singleLine="true".
  2. Add android:nextFocusDown="@+id/tv2" on your XML.
  3. Don't add android:imeOptions="actionNext", otherwise it will override above two attributes.
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
Shivam Yadav
  • 958
  • 11
  • 23
2

use this attribute:

android:inputType="textPersonName"
bebosh
  • 806
  • 10
  • 25
2

Add

android:inputType="text"

line in your edittext tag. and your problem will be resolved.

mehmoodnisar125
  • 1,469
  • 18
  • 14
1

Just add android:singleLine="true" to the EditText tag in the layout XML..

sumit pandey
  • 1,223
  • 1
  • 15
  • 23
1

Just add android:singleLine="true" you don't need that code for that

Arpit Patel
  • 7,212
  • 5
  • 56
  • 67
0

android:singleLine="true" is deprecated

use

 android:maxLines="1"
 android:inputType="text"

instead

Manohar
  • 22,116
  • 9
  • 108
  • 144