114

I've used some apps where when I fill my username, then go to my password, if I hit "Done" on the keyboard, the login form is automatically submitted, without me having to click the submit button. How is this done?

Kaloyan Roussev
  • 14,515
  • 21
  • 98
  • 180
  • Try this link answer... [insert the data in database when keyboard's done button is click](http://stackoverflow.com/questions/15244123/insert-the-data-in-database-when-keyboards-done-button-is-click-in-android) – AmmY Oct 07 '13 at 05:30
  • Quick link to the docs: [Specify the Input Method Action](https://developer.android.com/training/keyboard-input/style.html#Action) – FirstOne Oct 11 '17 at 12:57

10 Answers10

218

Try this:

In your layout put/edit this:

<EditText
    android:id="@+id/search_edit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:singleLine="true"
    android:imeOptions="actionDone" />

In your activity put this (e. g. in onCreate):

 // your text box
 EditText edit_txt = (EditText) findViewById(R.id.search_edit);

 edit_txt.setOnEditorActionListener(new EditText.OnEditorActionListener() {
     @Override
     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
         if (actionId == EditorInfo.IME_ACTION_DONE) {
             submit_btn.performClick();
             return true;
         }
         return false;
     }
 });

Where submit_btn is your submit button with your onclick handler attached.

m02ph3u5
  • 3,022
  • 7
  • 38
  • 51
Hariharan
  • 24,741
  • 6
  • 50
  • 54
  • 16
    `submit_btn.performClick();` is burning my eyes. Srsly? Why not call the submit method? – Laurent Meyer Oct 18 '16 at 16:30
  • 32
    @LaurentMeyer Simulating user input is usually better than directly calling the underlying logic in these situations. For example, the submit button might be disabled currently, so performClick() would do nothing (as intended), but if you called the submit method directly, you'd have to check that the button wasn't disabled first. It will also play the "click" sound as though the button were tapped, etc. – Extragorey May 01 '17 at 23:47
  • 3
    @LaurentMeyer What do you mean by UI sensitive? And 5 people in the last 6 months, sure. Give them time and people will probably agree with me too. ;) – Extragorey May 03 '17 at 01:59
  • Let's consider that you change UI, that use the button for something else. The code will be a real mess and even worse, you need to have really extensive test procedures to detect that kind of bug. Even worse is when you share UI component with such practices. – Laurent Meyer May 03 '17 at 10:06
  • Although an answer that sets you in the right direction, "put this and put that" doesn't really explain anything.. For more info: [Specify the Input Method Action](https://developer.android.com/training/keyboard-input/style.html#Action). – FirstOne Oct 11 '17 at 12:56
  • 2
    TWIMC, using `imeActionLabel` in my EditText was disabling all this behavior. Watch out – Alwin Kesler Mar 15 '18 at 00:42
  • Remember add android:maxLines="1" & android:inputType="text" to your EditText. – Omid Omidi Oct 13 '21 at 19:52
34

Simple and effective solution with Kotlin

Extend EditText:

fun EditText.onSubmit(func: () -> Unit) {
    setOnEditorActionListener { _, actionId, _ ->

       if (actionId == EditorInfo.IME_ACTION_DONE) {
           func()
       }

       true

    }
}

Then use the new method like this:

editText.onSubmit { submit() }

Where submit() is something like this:

fun submit() {
    // call to api
}

More generic extension

fun EditText.on(actionId: Int, func: () -> Unit) {
    setOnEditorActionListener { _, receivedActionId, _ ->

       if (actionId == receivedActionId) {
           func()
       }

        true
    }
}

And then you can use it to listen to your event:

email.on(EditorInfo.IME_ACTION_NEXT, { confirm() })
29

You need to set the IME Options on your EditText.

<EditText
    android:id="@+id/some_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Whatever"
    android:inputType="text"
    android:imeOptions="actionDone" />

Then add a OnEditorActionListener to the view to listen for the "done" action.

EditText editText = (EditText) findViewById(R.id.some_view);
editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            // TODO do something
            handled = true;
        }
        return handled;
    }
});

Official API doc: https://developer.android.com/guide/topics/ui/controls/text.html#ActionEvent

Vivek Warde
  • 1,936
  • 8
  • 47
  • 77
flx
  • 14,146
  • 11
  • 55
  • 70
6

This is how it is done

editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if(actionId == EditorInfo.IME_ACTION_DONE){
            //do something
        }
        return false;
   }
});

Don't forget to add following

<EditText android:layout_height="wrap_content"

    android:layout_width="wrap_content"

    android:imeOptions="actionDone"/>

actionDone in your EditText.

Gary Chen
  • 248
  • 2
  • 14
Jitender Dev
  • 6,907
  • 2
  • 24
  • 35
2

In your XML file inside your edittext tag add below snippet

android:imeOptions="actionDone"

Then inside your Java class, write the below code

editText.setOnEditorActionListener(new EditText.OnEditorActionListener() { 
    @Override 
    public boolean onEditorAction(TextView v, int id, KeyEvent event) { 
        if (id == EditorInfo.IME_ACTION_DONE) { 
            //do something here 
            return true;
        }
        return false; 
    } 
});
Gary Chen
  • 248
  • 2
  • 14
Madhu Kumar
  • 175
  • 1
  • 4
  • 15
2

Just extend this answer

fun EditText.onSubmit(func: () -> Unit) {
    setOnEditorActionListener { _, actionId, _ ->
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            clearFocus() // if needed 
            hideKeyboard()
            func()
        }
        true
    }
}

fun EditText.hideKeyboard() {
    val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(this.windowToken, 0)
}
bitvale
  • 1,959
  • 1
  • 20
  • 27
1

add the following line in edittext

android:imeOptions="actionDone"

Happy coding

Senthil JS
  • 31
  • 6
1
etParola = (EditText) findViewById(R.id.etParola); 
 btnGiris = (Button) findViewById(R.id.btnGiris);
  etParola.setOnEditorActionListener(new EditText.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    btnGiris.performClick();
                    return true;
                }
                return false;
            }
        });

 and;


layout xml etParola
android:imeOptions="actionDone" add
  • This exactly the same answer as [this one](https://stackoverflow.com/a/52294386). You should explain a bit how you think this solves OP's problem. – Adrian W Sep 12 '18 at 12:17
0
<EditText
    android:id="@+id/signinscr_userName"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/userName"
    android:imeOptions="actionNext" />

<EditText
    android:id="@+id/signinscr_password"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/password"
    android:imeOptions="actionDone"
    android:inputType="textPassword" />

in the java file

EditText userNameField = (EditText) findViewById(R.id.signinscr_userName);
EditText passwordField = (EditText) findViewById(R.id.signinscr_password);

passwordField.setOnEditorActionListener(new OnEditorActionListener() {
    public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
        //Do your operation here.
        return false;
    }
});
Gary Chen
  • 248
  • 2
  • 14
Amit Gupta
  • 8,914
  • 1
  • 25
  • 33
0
 EditText edit_txt = (EditText) findViewById(R.id.search_edit);

 edit_txt.setOnEditorActionListener(new EditText.OnEditorActionListener() {
     @Override
     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// which is u had set a imeoption
         if (actionId == EditorInfo.IME_ACTION_DONE) {
             submit_btn.performClick();
             return true;
         }
         return false;
     }
 });