Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicit "Submit" after hitting Done on the keyboard at the last EditText

Tags:

android

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?

like image 574
Kaloyan Roussev Avatar asked Oct 07 '13 05:10

Kaloyan Roussev


2 Answers

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.

like image 77
Hariharan Avatar answered Sep 28 '22 00:09

Hariharan


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

like image 28
flx Avatar answered Sep 28 '22 01:09

flx