Implicit "Submit" after clicking Finish on the keyboard in the last EditText

I used some applications where, when I fill out my username, and then go to my password, if I click Finish on the keyboard, the login form will be automatically sent without having to click the Submit button. How it's done?

+88
android
07 Oct '13 at 5:23
source share
10 answers

Try the following:

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 an onclick handler attached.

+167
Oct 07 '13 at 5:27
source share

You need to set the IME parameters to 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 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 doc API: https://developer.android.com/guide/topics/ui/controls/text.html#ActionEvent

+24
Oct 07 '13 at 5:27
source share

A 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 as follows:

 editText.onSubmit { submit() } 

Where submit() looks something like this:

 fun submit() { // call to api } 

More general 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() }) 
+18
Feb 15 '18 at 15:03
source share

Here's how to do it.

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

Do not forget to add

 <EditText android:layout_height="wrap_content" android:layout_width="wrap_content" android:imeOptions="actionDone"/> 

actionDone in your EditText.

+6
07 Oct '13 at 5:29
source share

In your XML file inside your edittext tag add below snippet

 android:imeOptions="actionDone" 

Then in your Java class write the code below

 editText.setOnEditorActionListener(new EditText.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int id, KeyEvent event) { if (id == EditorInfo.IME_ACTION_DONE) { //do your work here return true; } return false; } }); 
+2
Feb 15 '18 at 16:00
source share

add the following line to edittext

 android:imeOptions="actionDone" 

Happy coding

+1
Apr 20 '18 at 7:17
source share
 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 
+1
Sep 12 '18 at 11:42 on
source share

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) } 
+1
Sep 17 '19 at 21:08
source share
 <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 a .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; } }); 
0
Oct 07 '13 at 5:35 on
source share
  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; } }); 
0
Apr 30 '16 at 4:51 on
source share



All Articles