38

My layout has 4 EditText views and a submit Button view. I need to have "Next" button for the first 3 EditText's and a "Done" button for 4th EditText field in place of a "New Line" key of soft keyboard.

How can this be done?

CJBS
  • 15,147
  • 6
  • 86
  • 135
Venkat Papana
  • 4,757
  • 13
  • 52
  • 74

4 Answers4

73

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.

See: android:imeOptions documentation

Also, there's a small XML example in the training docs.

Martin Stone
  • 12,682
  • 2
  • 39
  • 53
  • 5
    Hi Martin, android:imeOptions="actionNext" is working fine for the EditText's with android:inputType="text", But for EditText with android:inputType="textMultiLine" not. – Venkat Papana May 15 '11 at 09:52
  • @Venkat, you could try `flagNavigateNext` instead: "...allows the IME to be multiline (with an enter key) as well as provide forward navigation..." (see first link above). – Martin Stone May 15 '11 at 09:54
  • Hi Martin, i'm gettting "Error: string types not allowed" by using android:imeOptions="flagNavigateNext" for EditText – Venkat Papana May 15 '11 at 10:03
  • I get the same here :-( -- Looks like it's only available in API Level 11 or higher. (The documentation filter seems to be broken on that page, but is working for the corresponding Java constants here: http://developer.android.com/reference/android/view/inputmethod/EditorInfo.html) – Martin Stone May 15 '11 at 10:18
  • 1
    Hi Martin, then cant we add "Next" button for multiline EditText? – Venkat Papana May 18 '11 at 18:43
  • 1
    It would seem not. Even with API Level 11, the keyboard doesn't have to obey flagNavigateNext if it doesn't have space. Looking at a few apps on my phone, it seems common to have the multiline box last, with a visible "Done" or "Send" button below it (e.g. Email app). – Martin Stone May 19 '11 at 08:36
  • Thanks for the training docs link, I realised I was looking for `EditorAction`. I managed to get the Done keyboard button to execute my login function like this on Xamarin: `var passwordField = FindViewById (Resource.Id.passwordField);` `passwordField.EditorAction += (sender, args) => { if (args.ActionId == ImeAction.Done) { // login action` `};` [This answer helped me alot too](http://stackoverflow.com/questions/16317083/edittext-setoneditoractionlistener) – GabLeRoux Nov 10 '14 at 20:27
6

to navigate the focus to the next edit field add

android:imeOptions="flagNavigateNext"

and for dismissing the softkey with done click add

android:imeOptions="actionDone"

on your layout :)

jibysthomas
  • 1,601
  • 2
  • 16
  • 24
3

Add android:singleLine="true" in your button xml

Aneh Thakur
  • 1,072
  • 12
  • 20
1

I think what you're looking for is something like this:

EditText nextText = new EditText(this)
{
    @Override
    public InputConnection onCreateInputConnection(final EditorInfo outAttrs)
    {
        outAttrs.imeOptions |= EditorInfo.IME_ACTION_NEXT;
        return (super.onCreateInputConnection(outAttrs));
    }
};

EditText doneText = new EditText(this)
{
    @Override
    public InputConnection onCreateInputConnection(final EditorInfo outAttrs)
    {
        outAttrs.imeOptions |= EditorInfo.IME_ACTION_DONE;
        return (super.onCreateInputConnection(outAttrs));
    }
};

It first one will take the user to the next field in the layout that accepts text. The second will close the IME (the soft keyboard).

Gyan aka Gary Buyn
  • 12,242
  • 2
  • 23
  • 26